Read File Java 7 Using BufferedReader

Read File Java 7 Using BufferedReader

In this demo example we will text file in java using BufferedReader and print its output on the console:

Java Class Example

package com.javahonk;

import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ReadFileUsingBufferedReader {
	
	private final static String FILE_NAME = "C:\\JavaHonk\\File\\JavaHonk.txt";
	private final static Charset ENCODING = StandardCharsets.UTF_8;
	
	public static void main(String args[]) throws IOException {
		Path path = Paths.get(FILE_NAME);
		try (BufferedReader bufferedReader = Files.newBufferedReader(path, ENCODING)) {
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				writeOutputOnConsole(line);
			}
		}	
	}
	
	private static void writeOutputOnConsole(Object value) {
		System.out.println(String.valueOf(value));
	}
	
}

Output:

Read File Java 7 Using BufferedReader

For more information write to file using java please refer oracle documentation here

Leave a Reply

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