Java examples for File Path IO:Text File
Reading all of the lines of a file returned as a list
import java.io.IOException; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.List; public class Main { public static void main(String[] args) throws IOException { try {/*ww w .j a va 2 s . com*/ Path path = Paths.get("/home/docs/users.txt"); List<String> contents = Files.readAllLines(path, Charset.defaultCharset()); for (String b : contents) { System.out.println(b); } } catch (IOException e) { e.printStackTrace(); } } }
Notice that the strings returned from the readAllLines method does not include the end of line character.
The readAllLines method recognizes the following line terminators:
\u000D followed by \u000A (CR/LF) \u000A, (LF) \u000D, (CR)