In: Computer Science
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.
I have used FileReader and BufferedWriter to successfully read and write in append mode to the file. Java BufferedWriter class is used to provide buffering for Writer instances. It makes the performance fast. It inherits Writer class. The buffering characters are used for providing the efficient writing of single arrays, characters, and strings.
Java FileReader class is used to read data from the file. It returns data in byte format like FileInputStream class. It is character-oriented class which is used for file handling in java.
I have created two classes namely, fileIO and fileHandling. In fileIO class, I have defined two functions. One functions is used here to open the file in append mode and write the user declared string to the file. Other function is used here to open the file in read mode and print whatever content is written in it. In fileHandling class, I have prompted the user to write to file, read from file, exit the program. As soon the user enters the option, their respective switch case block is executed and functions created in fileIO are called from there.
Here is the code for the same:-
import java.io.FileWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.BufferedWriter;
import java.util.*;
class fileIO{
public void WriteToFile(String fname,String str)
throws IOException{
try {
// Open given file in append mode.
BufferedWriter out = new BufferedWriter(new FileWriter(fname,
true));
out.write(str); //Write to the file
out.close(); //Closing the file
System.out.println("Writing successful...");
//Print Acknowledgement
}
catch (IOException e)
{
System.out.println("exception occoured" + e); //In case error
occured
}
}
public void ReadFromFile(String fileName)
throws IOException{
// variable
declaration
int ch;
// check if File exists
or not
FileReader
fr=null;
try
{
fr = new FileReader(fileName);
}
catch
(FileNotFoundException fe)
{
System.out.println("File not found!"); //in case exception
occur
}
System.out.print(""); //for Proper Output
Formatting
// read from FileReader
till the end of file
while
((ch=fr.read())!=-1)
System.out.print((char)ch);
// close the file
fr.close();
System.out.println("");
//for Proper Output Formatting
System.out.println("");
//for Proper Output Formatting
}
}
class fileHandling{
public static void main(String args[])throws
IOException{
char
ch='y';
int choice;
//For taking input choice from user
Scanner scr=new
Scanner(System.in); //Scanner object to
take input
fileIO f1 = new
fileIO();
//Creating an object of fileIO class to call its functions
do{
System.out.println("*** File Handling ***");
System.out.println("1. Wrtite to File");
System.out.println("2. Read from File");
System.out.println("3. Exit the Program");
System.out.println("Enter Your Choice...");
//nextLine() to read next line from the user
choice=Integer.parseInt(scr.nextLine());
//parseInt() is use to convert string to integer
switch(choice){
case 1:
System.out.println("Enter File Name : ");
String file;
file=scr.nextLine(); //reading File name
from the user
System.out.println("Enter string to be written : ");
String str;
str=scr.nextLine(); //read String to be
written in the file
f1.WriteToFile(file,str); //Calling function to write in the
file
System.out.println("");
break;
case 2:
System.out.println("Enter File Name : ");
String file1;
file1=scr.nextLine(); //taking the input of file name
to be opened in Read mode
f1.ReadFromFile(file1); //Calling the function to read from the
file
break;
case 3:
System.out.println("Exiting the Program"); //Exiting
the program
return; //main ends if case 3 runs
default:
System.out.println("Wrong Choice! Try Again...");
System.out.println(""); //for Proper Output Formatting
}
}while(ch=='y');
scr.close();
}
//main ends here
} //fileHandling ends here
The sample output for the same is:-
Hope this helps you. Feel free to ask queries in the comment section.
Happy Learning...