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...
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 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...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional...
Write a program in Java to: Read a file containing ones and zeros into a two-dimensional int array. Your program must (1) read the name of the file from the command-line arguments, (2) open the file, (3) check that each line has the same number of characters and (4) check that the only characters in a line are ones and zeros. If there is no such command-line argument or no such file, or if any of the checks fail, your...
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.
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
Write a Java program to read in the 10 numbers in the example file Book1.csv provided...
Write a Java program to read in the 10 numbers in the example file Book1.csv provided above. The program should sum all the numbers, find the lowest number, find the highest number, and computer the average. Upon completion of the processing, the program should write a new text file named stats.txt with the information found in the following format where xxx represents a number calculated above. The sum of the numbers is: xxx The lowest number is: xxx The highest...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT