Java 8+
Use Java NIO.2 features, including the Paths and Files utility classes.
Call Files.lines to get a stream a Stream of the lines read from the file. Defaults to UTF-8 character encoding, with option to specify character-encoding.
String fileName = "path/to/example.txt" ;
try
(
Stream<String> stream = Files.lines(Paths.get(fileName))
)
{
stream.forEach(System.out::println);
}
You have to place the Stream in a try-with-resource block to ensure the #close method is called on it, otherwise the underlying file handle is never closed until the garbage collector does it much later.
Earlier Java
A common pattern is to use BufferedReader.
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
You can read the data faster if you assume there is no character encoding. e.g. ASCII-7 but it won't make much difference. It is highly likely that what you do with the data will take much longer.
EDIT: A less common pattern to use which avoids the scope of line leaking.
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
// process the line.
}
// line is not visible here.
}
Answer from Peter Lawrey on Stack OverflowJava 8+
Use Java NIO.2 features, including the Paths and Files utility classes.
Call Files.lines to get a stream a Stream of the lines read from the file. Defaults to UTF-8 character encoding, with option to specify character-encoding.
String fileName = "path/to/example.txt" ;
try
(
Stream<String> stream = Files.lines(Paths.get(fileName))
)
{
stream.forEach(System.out::println);
}
You have to place the Stream in a try-with-resource block to ensure the #close method is called on it, otherwise the underlying file handle is never closed until the garbage collector does it much later.
Earlier Java
A common pattern is to use BufferedReader.
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
String line;
while ((line = br.readLine()) != null) {
// process the line.
}
}
You can read the data faster if you assume there is no character encoding. e.g. ASCII-7 but it won't make much difference. It is highly likely that what you do with the data will take much longer.
EDIT: A less common pattern to use which avoids the scope of line leaking.
try(BufferedReader br = new BufferedReader(new FileReader(file))) {
for(String line; (line = br.readLine()) != null; ) {
// process the line.
}
// line is not visible here.
}
Look at this blog:
- Java Read File Line by Line - Java Tutorial
The buffer size may be specified, or the default size may be used. The default is large enough for most purposes.
// Open the file
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
}
//Close the input stream
in.close();
You can map to an array by splitting each line, then call the method you want.
Files.lines(filePath)
.map(l -> l.split(" "))
.forEach(a -> createGraphe(a[0], a[1], a[2]));
The method lines() you are using already does what you expect it to do.
Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read first 100 lines then rest of the lines will not be loaded into memory, which results in better performance.
This is slightly different than Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.
Reference: https://javarevisited.blogspot.com/2015/07/3-ways-to-read-file-line-by-line-in.html