Reading and writing a file in java using FileWriter and FileReader

In this Java tutorial, first of all, we will learn about reading and writing a file in Java. We will also learn about FileWriter and FileReader classes in Java. Hence these classes fall under character streams. Character streams will allow us to read or write data character by character.

Definition of character stream in Java

Character streams work directly with Unicode characters. Hence they allow us to read or write data character wise. Hence at the top of the character stream hierarchies are the Reader and Writer abstract classes.

You may also like to learn, Reading and writing a file in Java using FileInputStream and FileOutputStream

Reader class in Java

The reader is also an abstract class that defines how to take character inputs. Almost all the methods will throw an IOException on error conditions.

Methods

  • int read()-returns an int representation of the next available char.
  • int read(char buffer[])- It will return the actual number of characters read.
  • abstract int read(char buffer[], int offset, int numChars)-It reads characters from the offset to the maximum number of characters.

FileReader in Java

The FileReader class is basically used to read the contents of the file. Consequently, it is inherited from the InputStreamReader class. Hence its two mostly used constructors are as follows:
FileReader(String file_Path)
FileReader(File file_object)

A SIMPLE EXAMPLE of FileReader class in Java

import java.io.*;
class FileReader_example{
public static void main(String args[])throws IOException
{
FileReader fr=new FileReader("xyz.txt");
int p;
while((p=fr.read())!=-1)
System.out.println((char)i);
fr.close();
}
}

Hence, if we consider that the file xyz.txt contains:

Hello i am java

OUTPUT:

Hello i am java

In this above example, we are creating an object of the FileReader class. Certainly here we are using read() function to read each and every character of the file. So the read function has return type int so we have to convert int to char before printing.

Writer class in Java

Similarly, Writer is also an abstract class that defines how to handle character outputs. Almost all the methods will throw an IOException on error conditions.

Methods

  • public void write(String s)- it writes the string into FileWriter
  •  void write(char[] c)-writes the char array into FileWriter
  • abstract void close()-closes FileWriter

 

FileWriter in Java

FileWriter creates a writer used to write a file. Consequently, it is inherited from the OutputStream class. Most commonly used constructors are:

FileWriter(String file_path)
FileWriter(String file_path,boolean append)
FileWriter(File file_object)

A SIMPLE EXAMPLE of FileWriter in Java

import java.io.*;
class Sample
{
public static void main(String args[])
{
try
{
FileWriter fw=new FileWriter("xyz.txt");
fw.write("hello how are you");
fw.close();
}catch(Exception e){ System.out.println(e);}
System.out.println("congo...");
}

OUTPUT:

congo...

 

Therefore in this above example, we are creating an object of the FileWriter class. Certainly here we are using write() function to write each and every character of the file.

You may also read,

Leave a Reply

Your email address will not be published. Required fields are marked *