Javaexercise.com

Java 11 String Lines Method - Split String Into Lines

Java 11 included a new method lines() to String class. This method breaks the string into lines based on line terminators. Line terminators can be a line feed character (\n), a carriage return character (\r), a carriage return followed immediately by a line feed (\r\n).

A line can be either a sequence of zero or more characters followed by a line terminator, or it is a sequence of one or more characters followed by the end of the string.

It returns a stream of lines extracted from the string, separated by line terminators.

String Lines Syntax

public Stream<String> lines()

 

  • It does not take any argument.

  • It returns stream of lines in the same order in which they occur.

  • The line doesn’t include line terminators (\n,\r).
  • It provides better performance than String split("\R").

  • If the string is empty, zero lines are returned.
  • It supports lazy loading and search fast for line terminators.

 

Java String Lines Method Example

In this example, a string contains three line terminators (\n). We are using String lines() method that will return a stream of three lines. Since it returns stream, so we are using foreach loop to iterate the stream.

import java.util.stream.Stream;

class Demo{
	public static void main(String[] args) {
		String str = "First line \nSecond line \nThird line";
		// Getting stream of lines
		Stream<String> str1 = str.lines();
		// iterating each stream
		str1.forEach(System.out::println);
	}
}

Output:

First line 
Second line 
Third line
 

Example : Count Lines

We can use stream's count method to get number of lines in the stream. The lines() method returns stream but to know, in how many lines string was broken, count method helps. See the below example.

import java.util.stream.Stream;

class Demo{
	public static void main(String[] args) {
		String str = "First line \nSecond line \nThird line";
		// Getting stream of lines
		Stream<String> str1 = str.lines();
		// iterating each stream
		str1.forEach(System.out::println);
		// reading lines count
		long n = str.lines().count();
		System.out.println("Total lines: "+n);
	}
}

Output:

First line 
Second line 
Third line
Total lines: 3
 

 

Example: Writing Stream Lines into File

In case, we want to write the result of line() method into external file then we can use below code that writes the stream line into the file. Notice, we are writing stream data that require functional style, so we used iterator, iterator and map to write the data.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;

class Demo{
	public static void main(String[] args) {
		String str = "First line \nSecond line \nThird line";
		// Getting stream of lines
		Stream<String> str1 = str.lines();
		// Writing lines to the file
	    try { 
	    	Files.write(Paths.get("/file-path/abc.txt"),
	    		     (Iterable<String>)str1.map(String::valueOf)::iterator);
	    }catch(IOException io) {
	    	System.out.println(io);
	    }
		// closing stream
	    str1.close();
	}
}

Open the file abc.txt and you will see the stream data is written there. 


Conclusion

Well, in this topic, we learnt about the new lines() method of String class. We discussed various scenarios by using examples and alternate way to get Stream of line from String.

If we missed something, you can suggest us at - info.javaexercise.com

 

Java 11 String Repeat Java 11 Check Blank String