Coders Packet

Read a plain text file or Create new file and add text using JAVA

By Satvik Singh Sengar

How to read a plain text file or create a new one and add text to it using the File, FileReader, and FileWriter classes of JAVA.

Here in this source code, we will be using the File, FileReader, and FileWriter classes of JAVA.

The file class of Java is part java.io package and helps in working with files and directories. There are several methods in the File class. Here, we will use createNewFile() and getName() methods. 

createNewFile() creates a new empty file on the given path only if a file of the same name does not exist on that path.

FileReader class reads the text from the file by specifying the pathname of that file. The close() method is used to close the FileReader class.

Filewriter class is used to add text to a file by using the write() method. 

We have to handle the exception in all three classes by using IOException as all three are part of the java.io package. We have to toString() method to give the exception details in the string format.

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Satvik Singh Sengar (satviksinghsengar)

Download packets of source code on Coders Packet