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 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.BufferedWriter class methods in Java Bufferreader class writes text to character-output stream, buffering characters.Thus, providing efficient writing of single array, character and strings. A buffer size needs to be specified, if not it takes Default value. An output is immediately set to the underlying character or byte stream by the 5 min read How to Read a File using Applet? In this article, we will learn how to read the content of the file using Java and display those contents using Applet. Approach UsedThe user should type the name of the file in the input field that asks for the filename in the output applet window. If the file is already present, it will display the 2 min read Read File Into an Array in Java In Java, we can store the content of the file into an array either by reading the file using a scanner or bufferedReader or FileReader or by using readAllLines method. To store the content of the file better use the collection storage type instead of a static array as we don't know the exact lines o 8 min read FileInputStream getChannel() Method in Java with Examples The getChannel() method is a part of Java.io.FileInputStream class. This method will return the unique FileChannel object associated with the file input stream. A channel obtained from the getChannel() method of the Java.io.FileInputStream instance will be "open for reading."getChannel() will retur 2 min read FileInputStream available() Method in Java with Examples The available() method of FileInputStream class is used to return the estimated number of remaining bytes that can be read from the input stream without blocking. This method returns the number of bytes remaining to read from the file. When a file is completely read, this function returns zero. Synt 3 min read Like