BufferedReader lines() in Java

lines(): This method is available in the java.io.BufferedReader class of Java.

Syntax:

Stream<String> java.io.BufferedReader.lines()

This method returns a Stream, the elements of which are lines read from this BufferedReader.

The Stream is lazily populated,i.e., read-only occurs during the terminal stream operation.

Parameters: NA

Returns: a Stream<String> providing the lines of text described by this BufferedReader.

Exceptions: NA

Approach

Java

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

public class BufferedReaderlines {
    public static void main(String[] args) throws IOException {

        FileReader fileReader = new FileReader("D:\\hello.txt");
        BufferedReader bufferedReader =
new BufferedReader(fileReader);

        Stream<String> stream = bufferedReader.lines();

        System.out.println(stream.count());
        bufferedReader.close();

    }
}

Output:

1


No comments:

Post a Comment