Sample content of test.txt
Hello
Stack
Over
Flow
com
Code to read from this text file using lines() and forEach() methods.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FileLambda {
public static void main(String args[]) {
Path path = Paths.of("/root/test.txt");
try (Stream<String> lines = Files.lines(path)) {
lines.forEach(s -> System.out.println(s));
} catch (IOException ex) {
// do something or re-throw...
}
}
}
Answer from pardeep131085 on Stack OverflowVideos
Sample content of test.txt
Hello
Stack
Over
Flow
com
Code to read from this text file using lines() and forEach() methods.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class FileLambda {
public static void main(String args[]) {
Path path = Paths.of("/root/test.txt");
try (Stream<String> lines = Files.lines(path)) {
lines.forEach(s -> System.out.println(s));
} catch (IOException ex) {
// do something or re-throw...
}
}
}
Avoid returning a list with:
List<String> lines = Files.readAllLines(path); //WARN
Be aware that the entire file is read when Files::readAllLines is called, with the resulting String array storing all of the contents of the file in memory at once.
Therefore, if the file is significantly large, you may encounter an OutOfMemoryError trying to load all of it into memory.
Use stream instead: Use Files.lines(Path) method that returns a Stream<String> object and does not suffer from this same issue.
The contents of the file are read and processed lazily, which means that only a small portion of the file is stored in memory at any given time.
Files.lines(path).forEach(System.out::println);
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
you can reference the java.nio.file.Path and the java.nio.file.Files class in java api.
you can get the answer about the question.
First step you should get the Path object by the file path.
Second step you can read line by line the content by the Files.readAllLines(Path path) method.
the example in the below:
Path path = FileSystems.getDefault().getPath(filePath);
List<String> datas = Files.readAllLines(path)
The approach you're going about seems a bit crude. There are many other ways to go about doing this. Many others have submitted answers, but here's an alternative one.
Also please note that as of Java 7, there is now automatic resource management for classes that implements the AutoCloseable interface.
With that being said, here's a way of doing it.
String[] lines = new String[11]; //11 players
try (BufferedReader reader = new BufferedReader(new FileReader("file path here"))) {
String line = null;
int index = 0;
while ((line = reader.readLine()) != null) {
lines[index++] = line;
}
} catch (IOException e) {
System.out.println(e.getMessage());
}