HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java 11 / String lines() – Get stream of lines – Java 11

String lines() – Get stream of lines – Java 11

Learn to convert multi-line string into stream of lines using String.lines() method in Java 11.

This method is useful when we want to read content from a file and process each string separately.

1. String.lines() API

The lines() method is a static method. It returns a stream of lines extracted from a given multi-line string, separated by line terminators.

/**
* returns - the stream of lines extracted from given string
*/
public Stream<String> lines()

A line terminator is one of the following –

  • a line feed character (“\n”)
  • a carriage return character (“\r”)
  • a carriage return followed immediately by a line feed (“\r\n”)

By definition, a line is zero or more character followed by a line terminator. A line does not include the line terminator.

The stream returned by lines() method contains the lines from this string in the same order in which they occur in the multi-line.

2. Java program to get stream of lines

Java program to read a file and get the content as stream of lines.

import java.io.IOException;
import java.util.stream.Stream;

public class Main 
{
	public static void main(String[] args) 
	{
		try 
		{
			String str = "A \n B \n C \n D"; 

			Stream<String> lines = str.lines();

			lines.forEach(System.out::println);
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
}

Program output.

A
B
C
D

Drop me your questions related to reading a string into lines of stream.

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java 11 Tutorial

  • Java 11 – New features
  • Java 11 – String.isBlank()
  • Java 11 – String.lines()
  • Java 11 – String.repeat()
  • Java 11 – String.strip()
  • Java 11 – Files.readString()
  • Java 11 – Files.writeString()

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)