java read text file

Let’s learn java read text file.

java read text file

To read text file line by line there are many ways while required apparently in several applications.

Here are few ways to read text file. First let’s read text file using BufferedReader.

BufferedReader creates a buffering character-input stream that uses a default-sized input buffer.

Also FileReader is used along with BufferedReader which Creates a new FileReader, given the File to read, using the platform’s default charset.

Lastly using while loop each line in text file is read. Now let’s see an example on how to read text file.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class JavaReadTextFileUsingBufferedReader
{
   public static void main(String[] args) throws IOException
   {
      File fl = new File("B:\\demo.txt");
      BufferedReader br = new BufferedReader(new FileReader(fl));
      String str;
      while((str = br.readLine()) != null)
      {
         System.out.println(str);
      }
      br.close();
   }
}


Output:

hello world java


read text file using FileReader class:

FileReader class creates a new FileReader, given the name of the file to read, using the platform’s default charset.

Here’s an example on how to read text file line by line using FileReader class.

import java.io.FileReader;
import java.io.IOException;
public class JavaReadTextFileUsingFileReader
{
   public static void main(String[] args) throws IOException
   {
      FileReader fr = new FileReader("B:\demo.txt");
      int a;
      while((a = fr.read()) != -1)
      {
         System.out.print((char) a);
      }
      fr.close();
   }
}


Output:

hello
world
core
java


java read file line by line scanner:

Here Scanner class constructs a new Scanner that produces values scanned from the specified file.

Bytes from the file are converted into characters using the underlying platform’s default charset.

Let’s see example on java read file line by line scanner.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class JavaReadTextFileScanner
{
   public static void main(String[] args) throws FileNotFoundException
   {
      File fl = new File("B:\demo.txt");
      Scanner sc = new Scanner(fl);
      while(sc.hasNextLine())
      {
         System.out.println(sc.nextLine());
      }
      sc.close();
   }
}


Output:

hello world core java


read text file as string:

Here’s an example to read text file as string in java.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class ReadTextFileAsStringJava
{
   public static void main(String[] args) throws Exception
   {
      String strInput = readFileString("B:\demo.txt");
      System.out.println(strInput);
   }
   public static String readFileString(String fileName) throws IOException
   {
      String strInput = "";
      strInput = new String(Files.readAllBytes(Paths.get(fileName)));
      return strInput;
   }
}


Output:

hello
world
core
java