All IT Courses 50% Off
JAVA Tutorials

Reading and Writing text file in Java

  1. Introduction to Reading a text File in Java:
  • There are many ways to read a plain text file in Java.
  • We are using FileReader, BufferedReader, and Scanner to read a text file. 
  • This utility provides something special like: 
    • BufferedReader: It provides buffering of data for fast reading. 
    • Scanner: It provides parsing ability.

A. BufferedReader: 

  • The BufferReader reads the text from a character-input stream.
  • It also did buffering for the reading of the characters, arrays, and the lines.

Let’s have an Example:

BufferedReader in = new BufferedReader(Reader in, int size);


public class ReadFile
 public static void main(String[] arg)throws Exception 
 File fi = new File("C:\\Users\\read\\Desktop\\test.txt"); 
 BufferedReader br = new BufferedReader(new FileReader(fi)); 
 String st; 
 while ((st = br.readLine()) != null) 

B. FileReader Class: It is a class for reading character files.

public class ReadingFile 
 public static void main(String[] arg) throws Exception 
 FileReader fileread= new FileReader("C:\\Users\\test\\Desktop\\test.txt"); 
 int i; 
 while (( != -1) 
 System.out.print((char) i); 

C. Scanner Class: 

  • A scanner which can parse the primitive types and also using strings through regular expressions.
  • A Scanner class can break its input into tokens, which by default matches whitespace. 
  • These resulting tokens may then be converted into values of different types using the various next methods.
// Java Program to illustrate reading from Text File 
// using Scanner Class 
import java.util.Scanner; 
public class ReadFromFileUsingScanner 
 public static void main(String[] arg) throws Exception 
 // pass the path to the file as a parameter 
 File file = new File("C:\\Users\\test\\Desktop\\test.txt"); 
 Scanner sc = new Scanner(file); 
 while (sc.hasNextLine()) 

2. Introduction to Writing a text File in Java:

a. Java BufferedWriter Class:

  • Java BufferedWriter class is used to provide buffering for Writer instances.
  • It makes the performance fast.
  • It inherits the Writer class.
  • The buffering characters are used for delivering the efficient writing of single arrays, characters, and strings.
  • Creates a buffered character-output stream that uses a default-sized output buffer.
public class BufferedWriter extends Writer


public class NewClass 
 public static void main(String[] args) 
 //initializing FileWriter 
 FileWriter filewrite; 
filewrite = new FileWriter("ABC.txt"); 
 // Initialing BufferedWriter 
 BufferedWriter bufferwrite = new BufferedWriter(filewrite); 
 System.out.println("Buffered Writer start writing :)"); 
 // Use of write() method to write the value in 'ABC' file 
 // Closing BufferWriter to end operation 
 System.out.println("Written successfully"); 
 catch (IOException excpt) 

b) Java FileWriter Class

  • This class is used to write character-oriented data to a file.
  • It is a character-oriented class which is used for file handling in java.
  • Unlike the FileOutputStream class, you don’t need to convert the string into a byte array because it provides a method to write string directly.
public class FileWriter extends OutputStreamWriter


 public class FileWriterExample { 
 public static void main(String args[]){ 
 FileWriter fw=new FileWriter("D:\\testout.txt"); 
catch(Exception e)
Facebook Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button