---
title: "FileWriter & FileReader Class in Java"  
description: "if you have to read and write the textual information Then Java has suggested not to use the FileInputStream and FileOutputStream classes ."  
author: "Anupam Mishra"  
published: 2015-12-03  
updated: 2019-11-27  
canonical: https://www.mindstick.com/articles/11933/filewriter-filereader-class-in-java  
category: "java"  
tags: ["java", "j2ee"]  
reading_time: 2 minutes  

---

# FileWriter & FileReader Class in Java

FileWriter and FileReader [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) is used to for read and write from a text files. Bothclasses are [character](https://www.mindstick.com/articles/23551/an-investigate-distinctive-seafood-restaurant-for-your-image-stamp-character)-oriented classes in [file handling](https://www.mindstick.com/forum/34585/file-handling) in java. If you working on textual information in the file then, it’s better approach.

##### FileWriter class:

In Java, FileWriter is a character [stream](https://www.mindstick.com/forum/1901/stream-not-writing-on-a-file) to write [data](https://www.mindstick.com/articles/326933/5-reasons-big-data-is-important-to-the-logistics-industry) to a file. We used the constructer for creating a file.

For example, we create a file with the help of FileWriter constructor and put the [string](https://www.mindstick.com/interview/22908/what-are-java-string-class-method) in the write() [method](https://www.mindstick.com/forum/166/webservice-method) directly.

> ```
>     import java.io.*;      class FileWriterEx{       public static void main(String args[]){        try{         FileWriter fw=new FileWriter("FileWriter.txt");
>        fw.write("My name is khan");
> fw.close();        }catch(Exception e){System.out.println(e);}
> System.out.println("Successfully Done!");      } }
> ```

##### Output:

Successfully Done!

## \

##### FileReader class:

In Java, FileReader is a character stream to read data from a file.\
For example, we working with [existing file](https://www.mindstick.com/forum/33417/how-to-append-text-to-an-existing-file-in-java) with the help of FileReader constructor and read the data from its read () method.

####

> ```
>     import java.io.*;      class FileReaderEx{       public static void main(String args[]){  try{      FileReader fr=new FileReader("FileWriter.txt");    int i;    while((i=fr.read())!=-1)
> System.out.print((char)i);   fr.close();    }catch(Exception
> e){System.out.println(e);}  }   }
> ```
>
> ####
>
> ####
>
> ####
>
> ####
>
> ####
>
> ####
>
> ####
>
> ####
>
> ####
>
> ####

##### Output:

My name is khan

---

Original Source: https://www.mindstick.com/articles/11933/filewriter-filereader-class-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
