Print

Read Text File with Buffered Reader Java

Using BufferedReader in java.io.File package, you can read a text file. See the example below. 

 

package com.seleniummaster.examplefile;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadTextFile {

    public static void main(String[] args)
    {
   System.out.println(ReadToString("test.txt"));
    }

    public static String ReadToString(String FileToText)
    {
        StringBuilder contents=new StringBuilder();
        String workingDir = System.getProperty("user.dir");
        File filePath = new File(workingDir + File.separator);
        System.out.println(filePath);

        try
        {
            BufferedReader br = new BufferedReader(new FileReader(filePath+File.separator+FileToText));
            String s;

            while ((s = br.readLine()) != null) {
                contents.append(s);
            }

        } catch (IOException e) {
            System.out.println("Cannot read the text file");
        }
        return contents.toString();
    }
}

Run the code and see the result

C:\seleniummaster
Hello TestSelenium MasterThis is a new test This is a new test