Sunday, November 13, 2016

Java 7 : File reading with NIO

Below is the code on easy to read the file in Java 7

package com.test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

public class ReadFile {

public static void main(String args[]){
Path file = Paths.get("C:/", "name","folder1","test.txt");
//Paths get method will take list of string as parameter and refer the documents
try {
List<String> lines = Files.readAllLines(file);
for(String line: lines ){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

No comments:

Post a Comment