Reading string line by line java

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.

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 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"; Streamlines = str.lines(); lines.forEach(System.out::println); > catch (IOException e) < e.printStackTrace(); >> >

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

Источник

How to read a file line by line in Java

Sometimes we want to read a file line by line to a string to process the content. A good example is reading a CSV file line by line and then splitting the line by comma ( , ) into multiple columns.

In Java, there are various options available to choose from when you need to read a file line by line.

The Scanner class presents the simplest way to read a file line by line in Java. We can use Scanner class to open a file and then read its content line by line. A Scanner breaks its input into tokens using a delimiter pattern, which is a new line in our case:

try  // open file to read Scanner scanner = new Scanner(new File("examplefile.txt")); // read until end of file (EOF) while (scanner.hasNextLine())  System.out.println(scanner.nextLine()); > // close the scanner scanner.close(); > catch (FileNotFoundException ex)  ex.printStackTrace(); > 

The hasNextLine() method returns true if there is another line in the input of this scanner without advancing the file read position. To read data and move on to the next line, we should use the nextLine() method. This method moves the scanner past the current line and returns the rest of the current line, excluding any line separator at the end. The read position is then set to the beginning of the next line. Since the nextLine() method continues to search through the input looking for a line separator, it may buffer all of the input searching for the line to skip if no line separators are present.

The BufferedReader class provides an efficient way to read characters, arrays, and lines from a character-input stream. As the name suggests, it buffers the characters up to 8MB (or 8192KB) which is large enough for most use cases. If the file you are reading is larger than the default buffer size, you can customize the default size:

BufferedReader br = new BufferedReader(new FileReader("foo.txt"), size); 

The BufferedReader constructor accepts a Reader instance (like FileReader , InputStreamReader ) as character-input stream source. Here is a simple example that shows how to use it for reading a file line by line:

try  // create a reader instance BufferedReader br = new BufferedReader(new FileReader("examplefile.txt")); // read until end of file String line; while ((line = br.readLine()) != null)  System.out.println(line); > // close the reader br.close(); > catch (IOException ex)  ex.printStackTrace(); > 

The readLine() method reads a line of text from the file and returns a string containing the contents of the line, excluding any line-termination characters or null.

Note: A null value does not mean that the string is empty. Rather it shows that the end of the file is reached.

Alternatively, you can use lines() method from BufferedReader class that returns a Stream of lines. You can easily convert this stream into a list or read the lines like the following:

try  // create a reader instance BufferedReader br = new BufferedReader(new FileReader("examplefile.txt")); // list of lines ListString> list = new ArrayList>(); // convert stream into list list = br.lines().collect(Collectors.toList()); // print all lines list.forEach(System.out::println); // close the reader br.close(); > catch (IOException ex)  ex.printStackTrace(); > 

Java 8 Stream is another way (albeit cleaner) of reading a file line by line. We can use Files.lines() static method to initialize a lines stream like below:

try  // initialize lines stream StreamString> stream = Files.lines(Paths.get("examplefile.txt")); // read lines stream.forEach(System.out::println); // close the stream stream.close(); > catch (IOException ex)  ex.printStackTrace(); > 

In addition to simple API, streams are very useful for filtering, sorting and processing the data. Let us extend the above example and filter out the lines that end with a colon ( : ), then sort them alphabetically, and convert to uppercase:

try  // initialize lines stream StreamString> stream = Files.lines(Paths.get("examplefile.txt")); // apply filter & sorting stream.filter(l -> l.endsWith(":")) .sorted() .map(String::toUpperCase) .forEach(System.out::println); // close the stream stream.close(); > catch (IOException ex)  ex.printStackTrace(); > 

Java New I/O API or NIO (classes in java.nio.* package) provides the Files.readAllLines() method to read a text file line by line into a List , as shown below:

try  // read all lines ListString> lines = Files.readAllLines(Paths.get("examplefile.txt")); // print all lines lines.forEach(System.out::println); > catch (IOException ex)  ex.printStackTrace(); > 

The RandomAccessFile class provides a non-blocking mode of reading and writing files. A random-access file behaves like a large array of bytes stored in the file system. We can use RandomAccessFile to open a file in reading mode and then use its readLine() method to read line by line:

try  // open file in read mode RandomAccessFile file = new RandomAccessFile("examplefile.txt", "r"); // read until end of file String line; while ((line = file.readLine()) != null)  System.out.println(line); > // close the file file.close(); > catch (IOException ex)  ex.printStackTrace(); > 

The Apache Commons IO library contains utility classes, stream implementations, file filters, file comparators, and much more. Add the following to your build.gradle file to import the library in your project:

implementation 'commons-io:commons-io:2.6' 
dependency> groupId>commons-iogroupId> artifactId>commons-ioartifactId> version>2.6version> dependency> 

We can now use FileUtils.readLines() the static method from Apache Commons IO that reads all lines from a file into a List :

try  // read all lines of a file ListString> lines = FileUtils.readLines(Paths.get("examplefile.txt").toFile(), "UTF-8"); // process the lines for (String line : lines)  System.out.println(line); > > catch (IOException ex)  ex.printStackTrace(); > 

Since Apache Commons IO reads all lines from the file at once, it may not be a good solution for reading large files. It will continue blocking the for loop execution in the above case until all lines are added to the lines object.

Okie is another open-source I/O library developed by Square for Android, Kotlin, and Java. It complements native java.io and java.nio packages to make it much easier to access, save, and process the data. To import Okie in your project, add the following to the build.gradle file:

implementation 'com.squareup.okio:okio:2.4.0' 
dependency> groupId>com.squareup.okiogroupId> artifactId>okioartifactId> version>2.4.0version> dependency> 

Now we can use Okio.source() method to open a source stream to read a file. The returned Source interface is very small and has limited uses. Okie provides BufferedSource class to wrap the source with a buffer that makes your program run faster. Let us have an example:

try  // open a source stream Source source = Okio.source(Paths.get("examplefile.txt").toFile()); // wrap stream with a buffer BufferedSource bs = Okio.buffer(source); // read until end of file String line; while ((line = bs.readUtf8Line()) != null)  System.out.println(line); > // close the stream source.close(); > catch (IOException ex)  ex.printStackTrace(); > 

The readUtf8Line() method reads the data until the next line delimiter – either \n , \r\n , or the end of the file. It returns that data as a string, omitting the delimiter at the end. When it encounters empty lines, the method will return an empty string. If there isn’t no more data to read, it will return null .

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like.

Источник

How To Read a File Line-By-Line in Java

How To Read a File Line-By-Line in Java

In this article, you will learn about different ways to use Java to read the contents of a file line-by-line. This article uses methods from the following Java classes: java.io.BufferedReader , java.util.Scanner , Files.readAllLines() , and java.io.RandomAccessFile .

Reading a File Line-by-Line using BufferedReader

You can use the readLine() method from java.io.BufferedReader to read a file line-by-line to String. This method returns null when the end of the file is reached.

Here is an example program to read a file line-by-line with BufferedReader :

package com.journaldev.readfileslinebyline; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileLineByLineUsingBufferedReader  public static void main(String[] args)  BufferedReader reader; try  reader = new BufferedReader(new FileReader("sample.txt")); String line = reader.readLine(); while (line != null)  System.out.println(line); // read next line line = reader.readLine(); > reader.close(); > catch (IOException e)  e.printStackTrace(); > > > 

Continue your learning with the BufferedReader API Doc (Java SE 8).

Reading a File Line-by-Line using Scanner

You can use the Scanner class to open a file and then read its content line-by-line.

Here is an example program to read a file line-by-line with Scanner :

package com.journaldev.readfileslinebyline; import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ReadFileLineByLineUsingScanner  public static void main(String[] args)  try  Scanner scanner = new Scanner(new File("sample.txt")); while (scanner.hasNextLine())  System.out.println(scanner.nextLine()); > scanner.close(); > catch (FileNotFoundException e)  e.printStackTrace(); > > > 

Continue your learning with the Scanner API Doc (Java SE 8).

Reading a File Line-by-Line using Files

java.nio.file.Files is a utility class that contains various useful methods. The readAllLines() method can be used to read all the file lines into a list of strings.

Here is an example program to read a file line-by-line with Files :

package com.journaldev.readfileslinebyline; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class ReadFileLineByLineUsingFiles  public static void main(String[] args)  try  ListString> allLines = Files.readAllLines(Paths.get("sample.txt")); for (String line : allLines)  System.out.println(line); > > catch (IOException e)  e.printStackTrace(); > > > 

Continue your learning with the Files API Doc (Java SE 8).

Reading a File Line-by-Line using RandomAccessFile

You can use RandomAccessFile to open a file in read mode and then use its readLine method to read a file line-by-line.

Here is an example program to read a file line-by-line with RandomAccessFile :

package com.journaldev.readfileslinebyline; import java.io.IOException; import java.io.RandomAccessFile; public class ReadFileLineByLineUsingRandomAccessFile  public static void main(String[] args)  try  RandomAccessFile file = new RandomAccessFile("sample.txt", "r"); String str; while ((str = file.readLine()) != null)  System.out.println(str); > file.close(); > catch (IOException e)  e.printStackTrace(); > > > 

Continue your learning with the RandomAccessFile API Doc (Java SE 8).

Conclusion

In this article, you learned about different ways to use Java to read the contents of a file line-by-line.

Continue your learning with more Java tutorials.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Источник

Читайте также:  Проверить есть ли элемент массива python
Оцените статью