This is a Java Program to Perform Read and Write Operations for a File using Applet
Problem Description:
We have to write a program in Java such that it performs the read and write operations for a file using an applet.
Expected Input and Output:
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.
Problem Solution
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.
Program/Source Code
Here is source code of the Java Program to perform read & 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 */ import java.applet.*; import java.awt.*; import java.awt.event.*; import java.io.*; public class File extends Applet implements ActionListener { TextField file; TextArea text; //Initialize the applet public void init() { setBackground(Color.white); setLayout(null); Label label = new Label("File Name :"); label.setBounds(100,0,100,40); this.add(label); file = new TextField(); file.setBounds(200,0,200,40); this.add(file); text = new TextArea("",0,0,TextArea.SCROLLBARS_NONE); text.setBounds(50,75,400,300); this.add(text); Button read = new Button("Read From File"); read.setBounds(75,400,150,25); this.add(read); read.addActionListener(this); Button write = new Button("Write To File"); write.setBounds(275,400,150,25); this.add(write); write.addActionListener(this); } //Function to call functions for operations selected public void actionPerformed(ActionEvent e) { String button = e.getActionCommand(); if(button.equals("Read From File")) { read(); } else { write(); } } //Function to Read the File public void read() { try { FileReader obj = new FileReader(file.getText()); int i; text.setText(""); while((i=obj.read())!= -1) { text.setText(text.getText()+(char)i); } obj.close(); } catch(Exception E) { text.setText(E.getMessage()); } } //Function to Write to the File public void write() { try { FileWriter obj = new FileWriter(file.getText()); obj.write(text.getText()); obj.close(); text.setText("File Written Successfully"); } catch(Exception E) { text.setText(E.getMessage()); } } } /* <applet code = File.class width=500 height=500> </applet> */
To compile and execute the program type the following commands :
>>> javac File.java >>> appletviewer File.java
Program Explanation
1. To read the file use FileReader class.
2. To write to the file use FileWriter class.
Runtime Test Cases
Here’s the run time test cases for performing read and write for different input cases.
Test case 1 – To Test Write Operation.
Test case 2 – To Test Read Operation When File is Present.
Test case 3 – To Test Read Operation When File is Not Present.
This post is worth everyone’s attention. Where can I find out more?