Read File Java 7 Using nio Files

Read File Java 7 Using nio Files

In this example you will how read text file using JAVA 7 java.nio.file.Files and write output on console:

Java class example:

package com.javahonk;

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;
import java.util.List;

public class ReadFileUsingNIOFiles {
	
	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);
		List<String> lines = Files.readAllLines(path, ENCODING);
		for (String string : lines) {
			System.out.println(string);
		}		
	}
	
}

Output:

Read File Java 7 Using nio Files

 

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 *