File handling in Java using FileWriter and FileReader Last Updated : 11 Sep, 2023 Comments Improve Suggest changes Like Article Like Report Java FileWriter and FileReader classes are used to write and read data from text files (they are Character Stream classes). It is recommended not to use the FileInputStream and FileOutputStream classes if you have to read and write any textual information as these are Byte stream classes. FileWriterFileWriter is useful to create a file writing characters into it. This class inherits from the OutputStream class.The constructors of this class assume that the default character encoding and the default byte-buffer size are acceptable. To specify these values yourself, construct an OutputStreamWriter on a FileOutputStream. FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.FileWriter creates the output file if it is not present already. Constructors: FileWriter(File file) - Constructs a FileWriter object given a File object.FileWriter (File file, boolean append) - constructs a FileWriter object given a File object.FileWriter (FileDescriptor fd) - constructs a FileWriter object associated with a file descriptor.FileWriter (String fileName) - constructs a FileWriter object given a file name.FileWriter (String fileName, Boolean append) - Constructs a FileWriter object given a file name with a Boolean indicating whether or not to append the data written. Methods: public void write (int c) throws IOException - Writes a single character.public void write (char [] stir) throws IOException - Writes an array of characters.public void write(String str)throws IOException - Writes a string.public void write(String str, int off, int len)throws IOException - Writes a portion of a string. Here off is offset from which to start writing characters and len is the number of characters to write.public void flush() throws IOException flushes the streampublic void close() throws IOException flushes the stream first and then closes the writer. Reading and writing take place character by character, which increases the number of I/O operations and affects the performance of the system.BufferedWriter can be used along with FileWriter to improve the speed of execution.The following program depicts how to create a text file using FileWriter Java // Creating a text File using FileWriter import java.io.FileWriter; import java.io.IOException; class CreateFile { public static void main(String[] args) throws IOException { // Accept a string String str = "File Handling in Java using "+ " FileWriter and FileReader"; // attach a file to FileWriter FileWriter fw=new FileWriter("output.txt"); // read character wise from string and write // into FileWriter for (int i = 0; i < str.length(); i++) fw.write(str.charAt(i)); System.out.println("Writing successful"); //close the file fw.close(); } } FileReader FileReader is useful to read data in the form of characters from a ‘text’ file. This class inherited from the InputStreamReader Class.The constructors of this class assume that the default character encoding and the default byte-buffer size are appropriate. To specify these values yourself, construct an InputStreamReader on a FileInputStream. FileReader is meant for reading streams of characters. For reading streams of raw bytes, consider using a FileInputStream. Constructors: FileReader(File file) - Creates a FileReader , given the File to read fromFileReader(FileDescripter fd) - Creates a new FileReader , given the FileDescripter to read fromFileReader(String fileName) - Creates a new FileReader , given the name of the file to read from Methods: public int read () throws IOException - Reads a single character. This method will block until a character is available, an I/O error occurs, or the end of the stream is reached.public int read(char[] cbuff) throws IOException - Reads characters into an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached.public abstract int read(char[] buff, int off, int len) throws IOException -Reads characters into a portion of an array. This method will block until some input is available, an I/O error occurs, or the end of the stream is reached. Parameters: cbuf - Destination buffer off - Offset at which to start storing characters len - Maximum number of characters to read public void close() throws IOException closes the reader.public long skip(long n) throws IOException -Skips characters. This method will block until some characters are available, an I/O error occurs, or the end of the stream is reached. Parameters: n - The number of characters to skip The following program depicts how to read from the ‘text’ file using FileReader Java // Reading data from a file using FileReader import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; class ReadFile { public static void main(String[] args) throws IOException { // variable declaration int ch; // check if File exists or not FileReader fr=null; try { fr = new FileReader("text"); } catch (FileNotFoundException fe) { System.out.println("File not found"); } // read from FileReader till the end of file while ((ch=fr.read())!=-1) System.out.print((char)ch); // close the file fr.close(); } } Comment More infoAdvertise with us Next Article File handling in Java using FileWriter and FileReader N Nishant Sharma Improve Article Tags : Java Practice Tags : Java Similar Reads Java FileReader Class getEncoding() Method with Examples The getEncoding() method of FileReader Class in Java is used to return the name of the current stream's character encoding. If the stream is utilizing a historical encoding name, it will be returned; otherwise, the canonical encoding name of the stream will be returned. Syntax: public String getEnco 2 min read File Handling in Java In Java, with the help of File Class, we can work with files. This File Class is inside the java.io package. The File class can be used to create an object of the class and then specifying the name of the file.Why File Handling is Required?File Handling is an integral part of any programming languag 6 min read Difference Between BufferedReader and FileReader in Java BufferedReader and FileReader both classes are used to read data from a given character stream. Both of them have some pros and some cons. In this article, we will discuss the differences between them. Though the most important difference is in the way they work, but we will discuss the other detail 10 min read Difference Between FileInputStream and FileReader in Java Let us first do discuss them in order to get the understanding alongside an example to interpret the differences. Here first we will be discussing out FileReader class. So starting of with FileReader class in java is used to read data from the file. It returns data in byte format like FileInputStrea 4 min read Java.io.FilterWriter class in Java Abstract class for writing filtered character streams. The abstract class FilterWriter itself provides default methods that pass all requests to the contained stream. Subclasses of FilterWriter should override some of these methods and may also provide additional methods and fields. Constructor : pr 2 min read How to find and open the Hidden files in a Directory using Java Pre-requisites: Java File Handling So far the operations using Java programs are done on a prompt/terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information 3 min read How to Execute SQL File with Java using File and IO Streams? In many cases, we often find the need to execute SQL commands on a database using JDBC to load raw data. While there are command-line or GUI interfaces provided by the database vendor, sometimes we may need to manage the database command execution using external software. In this article, we will le 5 min read Difference Between FileInputStream and ObjectInputStream in Java FileInputStream class extracts input bytes from a file in a file system. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. It should be used to read byte-oriented data for example to read audio, video, images, 5 min read Java.io.FilterReader class in Java Abstract class for reading filtered character streams. The abstract class FilterReader itself provides default methods that pass all requests to the contained stream. Subclasses of FilterReader should override some of these methods and may also provide additional methods and fields. Constructor : pr 3 min read java.nio.file.FileSystem class in java java.nio.file.FileSystem class provides an interface to a file system. The file system acts as a factory for creating different objects like Path, PathMatcher, UserPrincipalLookupService, and WatchService. This object help to access the files and other objects in the file system. Syntax: Class decla 4 min read Like