Question

In: Computer Science

Write a java program that can create, read, and append a file. Assume that all data...

Write a java program that can create, read, and append a file. Assume that all data written to the file are string type. One driver class and a class that contains the methods.

The program should have three methods as follows:

CreateFile - only creating a file. Once it create a file successfully, it notifies to the user that it creates a file successfully.

ReadingFile - It reads a contents of the file. This method only allow to read a string from the file.

AppendFile - It opens an existing file and append string type data to the file.

The program should ask user a file name to be create, read, and appended. The program should also ask to user whether he/she wants to do other options.

Sample Output:

Do you want to work with file? (0 for yes, 1 for no): 0
What do you want to do?
0 for create a file
1 for Read a file
2 for Writing to a file
--> 0

Enter a file name: example.txt
example.txt file is successfully created!
Do you want to do more? (1 for yes, 0 for no): 1
What do you want to do?
0 for create a file
1 for Read a file
2 for Writing to a file
--> 2

Enter a file name: example.txt
Enter letters only: This is example...
More Input? (0 to continue, -1 to end): 0
Enter letters only: This is EX000 class... Bye!
More Input? (0 to continue, -1 to end): -1
Do you want to do more? (1 for yes, 0 for no): 1
What do you want to do?
0 for create a file
1 for Read a file
2 for Writing to a file
--> 1

Enter a file name: example.txt
This is example...
This is EX000 class... Bye!
Do you want to do more? (1 for yes, 0 for no): 0

Solutions

Expert Solution

import java.util.Scanner;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
class FileHandler
{
public void CreateFile(String fName) throws Exception
{
File f = new File(fName);
if (f.createNewFile())
{
System.out.println(f.getName()+" file is successfully created!");
}
else
{
System.out.println(f.getName()+" file already exists.");
}
}
public void ReadingFile(String fName) throws Exception
{

File f = new File(fName);
Scanner sc = new Scanner(f);
while (sc.hasNextLine())
{
String data = sc.nextLine();
System.out.println(data);
}
sc.close();
}
public void AppendFile(String fName) throws Exception
{
int choice;
Scanner sc = new Scanner(System.in);
FileWriter f = new FileWriter(fName);
do
{
System.out.print("Enter letters only: ");
f.write(sc.nextLine()+"\n");
System.out.print("More Input? (0 to continue, -1 to end): ");
choice = sc.nextInt();
sc.nextLine();
}while(choice==0);   
f.close();
}
}
public class Main
{
   public static void main(String[] args) throws Exception
   {
   FileHandler fh = new FileHandler();
   Scanner sc = new Scanner(System.in);
       System.out.print("Do you want to work with file? (0 for yes, 1 for no): ");
       int choice=sc.nextInt();
       if(choice==0)
       {
       do
       {
       System.out.print("What do you want to do?\n0 for create a file\n1 for Read a file\n2 for Writing to a file\n");
       int fChoice=sc.nextInt();
       System.out.print("Enter a file name: ");
       String fName= sc.next();
       if(fChoice==0)
       {
       fh.CreateFile(fName);
       }
       else if(fChoice==1)
       {
       fh.ReadingFile(fName);
       }
       else if(fChoice==2)
       {
       fh.AppendFile(fName);
       }
       else
       {
      
       }
       System.out.print("Do you want to do more? (1 for yes, 0 for no): ");
       choice=sc.nextInt();      
       }while(choice==1);
       }
   }
}


Related Solutions

(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does...
(Write/read data) Write a Program in BlueJ to create a file name Excersise12_15.txt if it does not exist. Write 100 integers created randomly into the file using text I/O. Integers are separated by spaces in the file. Read data back from the file and display the data in increasing order. After writing the file to disk, the input file should be read into an array, sorted using the static Arrays.sort() method from the Java API and then displayed in the...
Please write a java program to write to a text file and to read from a...
Please write a java program to write to a text file and to read from a text file.
Write a Fortran program that is able to read in the data file. The file has...
Write a Fortran program that is able to read in the data file. The file has lines with the structure: 19990122 88888 30.5 Where: i) the first is an 8 digit code with the date: yyyymmdd (yyyy is the year, mm is the month, and dd is the day) ii) the second is the five digit odometer reading of a car iii) the third is the amount of fuel put into the car on that date to fill the tank...
Write a Java program to read a set of integers from a file, dataX, and a...
Write a Java program to read a set of integers from a file, dataX, and a set of ranges from a second file, rangeX, and, for each range [a, b] in rangeX, report the SUM of all the integers in dataX which are in the range [a, b]. As the integers are read from file dataX, insert them in a binary search tree. After all the integers have been inserted into the binary search tree, read the ranges from file...
Write a java program that will read a file called stateinfo.txt and will store the information...
Write a java program that will read a file called stateinfo.txt and will store the information of the file into a map. The stateinfo.txt file contains the names of some states and their capitals. The format of stateinfo.txt file is as follows. State                Capital ---------                         ----------- NSW               Sydney VIC                 Melbourne WA                 Perth TAS                 Tasmania QLD                Brisbane SA                   Adelaide The program then prompts the user to enter the name of a state. Upon receiving the user input, the program should...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one...
JAVA Assignment: Project File Processing. Write a program that will read in from input file one line at a time until end of file and output the number of words in the line and the number of occurrences of each letter. Define a word to be any string of letters that is delimited at each end by either whitespace, a period, a comma or the beginning or end of the line. You can assume that the input consists entirely of...
Write a java program: Write a program that creates a text file. Write to the file...
Write a java program: Write a program that creates a text file. Write to the file three lines each line having a person's name. In the same program Append to the file one line of  'Kean University'.  In the same program then Read the file and print the four lines without lines between.
how to read, write and append a text file using a switch case in java.I need...
how to read, write and append a text file using a switch case in java.I need help with the code.
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
I need to write a java program (in eclipse) that will read my text file and...
I need to write a java program (in eclipse) that will read my text file and replace specific placeholders with information provided in a second text file. For this assignment I am given a text file and I must replace <N>, <A>, <G>, with the information in the second file. For example the information can be John 22 male, and the template will then be modified and saved into a new file or files (because there will be multiple entries...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT