This is a Java Program to Perform Read and Write Operations for a File using Applet
We have to write a program in Java such that it performs the read and write operations for a file using an applet.
For performing read and write operations, we can have the following different sets of input and output.
1. To Perform Read Operation : When the file is present in the directory.
When the user clicks on the read operation after entering the file name, then it is expected that the contents of file is viewed in the applet.
2. To Perform Read Operation: When the file is not present in the directory.
When the user clicks on the read operation after entering the file name, then it is expected that an appropriate message is displayed in the applet.
3. To Perform Write Operation: When the file is present in the directory.
When the user clicks on the write operation after entering the file name, then it is expected that the contents of the file is modified.
4. To Perform Write Operation: When the file is not present in the directory.
When the user clicks on the write operation after entering the file name, then it is expected that a file is created with the contents.
1. Create a text field to enter the file name.
2. Create two buttons for the read and write operations.
3. Create a text area when the contents of the file is displayed or written.
4. Use the FileReader class to read the file.
5. Use the FileWriter class to write to the file.
Here is source code of the Java Program to perform read and write operations. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.
/* Java Program to Perform Read and Write Operations */
public class File extends Applet implements ActionListener
//Initialize the applet
public void init()
Label label = new Label("File Name :");
file = new TextField();
text = new TextArea("",0,0,TextArea.SCROLLBARS_NONE);
Button read = new Button("Read From File");
Button write = new Button("Write To File");
//Function to call functions for operations selected
public void actionPerformed(ActionEvent e)
String button = e.getActionCommand();
if(button.equals("Read From File"))
//Function to Read the File
public void read()
FileReader obj = new FileReader(file.getText());
//Function to Write to the File
public void write()
FileWriter obj = new FileWriter(file.getText());
text.setText("File Written Successfully");
<applet code = File.class width=500 height=500>
To compile and execute the program type the following commands :
>>> javac File.java >>> appletviewer File.java
1. To read the file use FileReader class.
2. To write to the file use FileWriter class.
Here’s the run time test cases for performing read and write for different input cases.
Sanfoundry Global Education & Learning Series – Java Programs.Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!