Question

In: Computer Science

Create, and write, three people's names into an external file named "names.txt."  You should overwrite the file...

  1. Create, and write, three people's names into an external file named "names.txt."  You should overwrite the file if it already exists.
  2. Read those three people's names into the program from the text file and print them out to the end user's console.
  3. Append three more people's names to the external file named "names.txt."
  4. Read those six people's names from the text file into the program and print them out to the end user's console.

Then try:

  • Get the filename from the end user for all file I/O
  • Get the number of names from the end user for all file I/O
  • Use loops to get the names from the end user for all file I/O
  • Use a loop to repeat the program if the end user desires

***

ADDITIONAL NOTES IN CASE YOU ARE HAVING I/O TROUBLES:

//Writing from your Source Code to an External .txt File:
PrintWriter outputFile = new PrintWriter("names.txt");        
outputFile.println("Mike");
outputFile.close();

*************
//Appending from your Source Code to an External .txt File:
FileWriter fileWrite = new FileWriter("names.txt", true);
PrintWriter outWrite = new PrintWriter(fileWrite);
outWrite.println("John");
outWrite.close();

*************
//Reading from an external .txt File into your Source Code:
File myFile = new File("Customers.txt");
Scanner inputFile = new Scanner(myFile);
String str = inputFile.nextLine();
inputFile.close();

Solutions

Expert Solution

/**********************FileIO.java***************************/

import java.io.File;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Scanner;

public class FileIO {

   public static void main(String[] args) {

       Scanner scanner = new Scanner(System.in);
       char option = ' ';
       System.out.print("Enter File name: ");
       String fileName = scanner.nextLine();
       do {
           readFromFile(fileName);
           System.out.println("Enter the name you want to write: ");
           String name = scanner.nextLine();
           writeToFile(fileName, name);
           System.out.print("Do you want to try again(Y/N): ");
           option = scanner.nextLine().toUpperCase().charAt(0);
       } while (option != 'N');
       scanner.close();
   }

   private static void writeToFile(String string, String name) {
       try {
           FileWriter fileWrite = new FileWriter(string, true);
           PrintWriter outWrite = new PrintWriter(fileWrite);
           outWrite.println(name);
           outWrite.close();
       } catch (Exception e) {
           System.out.println("File not found!");
       }

   }

   private static void readFromFile(String filename) {
       try {
           File myFile = new File(filename);
           Scanner inputFile = new Scanner(myFile);
           while (inputFile.hasNextLine()) {
               String str = inputFile.nextLine();
               System.out.println(str);
           }
           inputFile.close();
       } catch (Exception e) {
           System.out.println("File not found!");
       }

   }
}
/*******************output*********************/

Enter File name: names.txt
Mike
John
Enter the name you want to write:
JKS
Do you want to try again(Y/N): Y
Mike
John
JKS
Enter the name you want to write:
MS Dhoni
Do you want to try again(Y/N): Y
Mike
John
JKS
MS Dhoni
Enter the name you want to write:
Jadeja
Do you want to try again(Y/N): N

Please let me know if you have any doubt or modify the answer, Thanks :)


Related Solutions

Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The...
Write a class named UpperCaseFile. The class's constructor should accept two file names as arguments. The first ofile should be opened for reading and the second file should be opened for writing. The class should read the contents of the first file, change all characters to uppercase, and store the results in the second file. The second file will be a copy of the first file, excpet all the characters will be uppercase. Use notepad or another text editor to...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on...
Create a bash script file named assessment-script-a that: 1. Accepts any number of file names on the command line 2. For each file name provided, delete any line that contains the string: qwe.rty When the changes are completed, the script should display the total number of files scanned, and the total number of files changed. Example Command: assessment-script-a file.a file.b file.c file.d file.e    Example Output: 5 files scanned, 3 files changed 3. Your script should include a series of...
Write scores of 20 students on a quiz to a file. The file should be named...
Write scores of 20 students on a quiz to a file. The file should be named scores.txt. The scores should be random numbers between 0-10. Next, read the scores from scores.txt and double them and print the scores to screen. c++
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant...
Create a file named StudentArrayList.java,within the file create a class named StudentArrayList. This class is meant to mimic the ArrayList data structure. It will hold an ordered list of items. This list should have a variable size, meaning an arbitrary number of items may be added to the list. Most importantly this class should implement the interface SimpleArrayList provided. Feel free to add as many other functions and methods as needed to your class to accomplish this task. In other...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using...
Write a program named FinalExamProgram2 that reads numbers from a file (which you will create using Notepad) into a one-dimensional array and then analyzes the numbers as described below. Your program must use loops to read the numbers into the array and to analyze the contents of the array. The program’s main function should do the following:  Read eight floating-point numbers from the file named numbers.txt into a onedimensional array, displaying each number on the screen.  Pass the...
Create a file named work.sh in your hw5 directory. Give this file read and write permission...
Create a file named work.sh in your hw5 directory. Give this file read and write permission (no execute permissions) for you alone. No other account should have any access privileges to this file. Change the permissions on the work.sh file so you have read and write permissions. Give everybody else, including the group, read permissions only. Give yourself read, write and execute permissions to the file work.sh. Give everyone else, including the group, execute permissions only. Create a directory named...
​​​​Python Create a new file named compute_cost.py. Write your name and date in a comment. Create...
​​​​Python Create a new file named compute_cost.py. Write your name and date in a comment. Create a variable named base_fee and set the value to 5.5. Prompt the user for the zone. The zones can be an integer value from 1 to any value for the zone. Convert the zone to an integer. Display the zone on the SenseHat with a scroll speed of 0.3, make the text blue, and the background yellow. Scroll "The zone is " and the...
1. Create a1. Create a new java file named Name.java that should have Name class based...
1. Create a1. Create a new java file named Name.java that should have Name class based on new java file named Name.java that should have Name class based on the following UML Name - first: String - last: String + Name () + Name (String firstName, String lastName) + getName () : String + setName (String firstName, String lastName) : void + getFirst () : String + setFirst (String firstName) : void + getLast () : String + setLast (String...
Consider a text file that you will create named “employees.txt”. The file contains data organized according...
Consider a text file that you will create named “employees.txt”. The file contains data organized according to the following format:John Smith 10 15Sarah Johnson 40 12Mary Taylor 27 13Jim Stewart 25 8For instance, “John” is the first name, “Smith” is the last name, “10” is the number of hours per week, and “15” is the hourly rate.Write a program that computes the weekly salary of each employee. The program prints the first name, last name, and weekly salary of each...
Using OOP, write a C++ program that will read in a file of names. The file...
Using OOP, write a C++ program that will read in a file of names. The file is called Names.txt and should be located in the current directory of your program. Read in and store the names into an array of 30 names. Sort the array using the selection sort or the bubblesort code found in your textbook. List the roster of students in ascending alphabetical order. Projects using global variables or not using a class and object will result in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT