Java Program to Perform Read and Write Operations for a File using Applet

«
»

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.

advertisement

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.

advertisement
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.

advertisement
Program/Source Code

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.

  1. /* Java Program to Perform Read and Write Operations */
  2. import java.applet.*;
  3. import java.awt.*;
  4. import java.awt.event.*;
  5. import java.io.*;
  6. public class File extends Applet implements ActionListener
  7. {
  8.     TextField file;
  9.     TextArea text;
  10.     //Initialize the applet
  11.     public void init()
  12.     {
  13. 	setBackground(Color.white);
  14. 	setLayout(null);
  15.  
  16. 	Label label = new Label("File Name :");
  17. 	label.setBounds(100,0,100,40);
  18. 	this.add(label);
  19.  
  20. 	file = new TextField();
  21. 	file.setBounds(200,0,200,40);
  22. 	this.add(file);
  23.  
  24. 	text = new TextArea("",0,0,TextArea.SCROLLBARS_NONE);
  25. 	text.setBounds(50,75,400,300);
  26. 	this.add(text);
  27.  
  28. 	Button read = new Button("Read From File");
  29. 	read.setBounds(75,400,150,25);
  30. 	this.add(read);
  31. 	read.addActionListener(this);
  32.  
  33. 	Button write = new Button("Write To File");
  34. 	write.setBounds(275,400,150,25);
  35. 	this.add(write);
  36. 	write.addActionListener(this);
  37.     }
  38.     //Function to call functions for operations selected
  39.     public void actionPerformed(ActionEvent e)
  40.     {
  41. 	String button = e.getActionCommand();
  42. 	if(button.equals("Read From File"))
  43. 	{
  44. 	    read();
  45. 	}
  46. 	else
  47. 	{
  48. 	    write();
  49. 	}
  50.     }
  51.     //Function to Read the File
  52.     public void read()
  53.     {
  54. 	try
  55. 	{
  56. 	    FileReader obj = new FileReader(file.getText());
  57. 	    int i;
  58. 	    text.setText("");
  59. 	    while((i=obj.read())!= -1)
  60. 	    {
  61. 		text.setText(text.getText()+(char)i);
  62. 	    }
  63. 	    obj.close();
  64. 	}
  65. 	catch(Exception E)
  66. 	{
  67. 	    text.setText(E.getMessage());		
  68. 	}
  69.     }
  70.     //Function to Write to the File
  71.     public void write()
  72.     {
  73. 	try
  74. 	{
  75. 	    FileWriter obj = new FileWriter(file.getText());
  76. 	    obj.write(text.getText());
  77. 	    obj.close();
  78. 	    text.setText("File Written Successfully");
  79. 	}
  80. 	catch(Exception E)
  81. 	{
  82. 	    text.setText(E.getMessage());
  83. 	}
  84.     }
  85. }
  86. /*
  87. <applet code = File.class width=500 height=500>
  88. </applet>
  89. */

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.

advertisement
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.
java-file-write-success

Test case 2 – To Test Read Operation When File is Present.
java-file-read-success

Test case 3 – To Test Read Operation When File is Not Present.
java-file-read-not-present

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!
advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn