Different ways of reading a text file in Java

There are multiple ways of writing and reading a text file in Java. this is required while dealing with many applications. There are several ways to read a plain text file in Java e.g. you can use FileReader, BufferedReader, or Scanner to read a text file.buffering reader also provides parsing and scanner to read the text file and several ways to read the text file in java.

code

import java.io.*;

import java.util.*;

public class ReadingTextFile

{

public static void main(String[] args) throws Exception

{

File file = new File("C:\\Users\\megh\\Documents\\test.txt");

BufferedReader input = new BufferedReader(new FileReader(file));

String str;

while ((str = input.readLine()) != null)

System.out.println(str);

}

}

 

output

Hello World 
Welcome to the house
Have a wonderful day

 

Leave a Comment

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

Scroll to Top