There is also Scanner. You can use it just like the BufferedReader:
Scanner scanner = new Scanner(myString);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// process the line
}
scanner.close();
I think that this is a bit cleaner approach that both of the suggested ones.
Answer from notnoop on Stack OverflowThere is also Scanner. You can use it just like the BufferedReader:
Scanner scanner = new Scanner(myString);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// process the line
}
scanner.close();
I think that this is a bit cleaner approach that both of the suggested ones.
You can also use the split method of String:
String[] lines = myString.split(System.getProperty("line.separator"));
This gives you all lines in a handy array.
I don't know about the performance of split. It uses regular expressions.
In Java, how do you make the Scanner read an entire line from the user?
string - How to read a file line by line with Java Stream - Stack Overflow
What is the best way to iterate over the lines of a Java String? - Stack Overflow
Need to read integer and String from a file in java
Videos
Trying to make a program which uses the scanner to read an entire series of characters entered by the user before they press enter.
So my code looks something like this: String input = sc.next();
Which prompts the user to enter something, and that gets stored into the string input.
But if the user enters something like - A B C D. In one line. The scanner only stores the A into the string.
How do I make it store the entire line?
When I try using sc.nextLine(), the program simply skips over the prompt. It doesn't prompt the user, it just skips that line and the string comes out empty.
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 could use :
BufferedReader bufReader = new BufferedReader(new StringReader(textContent));
And use the readLine() method :
String line = null;
while((line = bufReader.readLine()) != null) {
}
To add the Java 8 way to this question:
Arrays.stream(content.split("\\r?\\n")).forEach(line -> /*do something */)
Of curse you can also use System.lineSeparator()to split if you are sure that the file is comming from the same plattform as the vm runs on.
Or even better use the stream api even more agressiv with filter, map and collect:
String result = Arrays.stream(content.split(System.lineSeparator()))
.filter(/* filter for lines you are interested in*/)
.map(/*convert string*/)
.collect(Collectors.joining(";"));