In: Computer Science
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
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);
}
}
}