Reading text file in java line by line

Java 8 Stream – Read a file line by line

In Java 8, you can use Files.lines to read file as Stream .

 line1 line2 line3 line4 line5 

1. Java 8 Read File + Stream

 package com.mkyong.java8; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class TestReadFile < public static void main(String args[]) < String fileName = "c://lines.txt"; //read file into stream, try-with-resources try (Streamstream = Files.lines(Paths.get(fileName))) < stream.forEach(System.out::println); >catch (IOException e) < e.printStackTrace(); >> > 
 line1 line2 line3 line4 line5 

2. Java 8 Read File + Stream + Extra

This example shows you how to use Stream to filter content, convert the entire content to upper case and return it as a List .

 package com.mkyong.java8; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; import java.util.stream.Stream; public class TestReadFile2 < public static void main(String args[]) < String fileName = "c://lines.txt"; Listlist = new ArrayList<>(); try (Stream stream = Files.lines(Paths.get(fileName))) < //1. filter line 3 //2. convert all content to upper case //3. convert it into a List list = stream .filter(line ->!line.startsWith("line3")) .map(String::toUpperCase) .collect(Collectors.toList()); > catch (IOException e) < e.printStackTrace(); >list.forEach(System.out::println); > > 

3. BufferedReader + Stream

A new method lines() has been added since 1.8, it lets BufferedReader returns content as Stream .

 package com.mkyong.java8; import java.io.BufferedReader; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; public class TestReadFile3 < public static void main(String args[]) < String fileName = "c://lines.txt"; Listlist = new ArrayList<>(); try (BufferedReader br = Files.newBufferedReader(Paths.get(fileName))) < //br returns as stream and convert it into a List list = br.lines().collect(Collectors.toList()); >catch (IOException e) < e.printStackTrace(); >list.forEach(System.out::println); > > 
 line1 line2 line3 line4 line5 

4. Classic BufferedReader And Scanner

Enough of Java 8 and Stream , let revisit the classic BufferedReader (JDK1.1) and Scanner (JDK1.5) examples to read a file line by line, it is working still, just developers are moving toward Stream .

4.1 BufferedReader + try-with-resources example.

 package com.mkyong.core; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class TestReadFile4 < public static void main(String args[]) < String fileName = "c://lines.txt"; try (BufferedReader br = new BufferedReader(new FileReader(fileName))) < String line; while ((line = br.readLine()) != null) < System.out.println(line); >> catch (IOException e) < e.printStackTrace(); >> > 

4.2 Scanner + try-with-resources example.

 package com.mkyong.core; import java.io.File; import java.io.IOException; import java.util.Scanner; public class TestReadFile5 < public static void main(String args[]) < String fileName = "c://lines.txt"; try (Scanner scanner = new Scanner(new File(fileName))) < while (scanner.hasNext())< System.out.println(scanner.nextLine()); >> catch (IOException e) < e.printStackTrace(); >> > 

References

mkyong

Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Читайте также:  Способы задания литералов различных типов java

Источник

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.

Источник

Reading text file in java line by line

Learn Latest Tutorials

Splunk tutorial

SPSS tutorial

Swagger tutorial

T-SQL tutorial

Tumblr tutorial

React tutorial

Regex tutorial

Reinforcement learning tutorial

R Programming tutorial

RxJS tutorial

React Native tutorial

Python Design Patterns

Python Pillow tutorial

Python Turtle tutorial

Keras tutorial

Preparation

Aptitude

Logical Reasoning

Verbal Ability

Company Interview Questions

Artificial Intelligence

AWS Tutorial

Selenium tutorial

Cloud Computing

Hadoop tutorial

ReactJS Tutorial

Data Science Tutorial

Angular 7 Tutorial

Blockchain Tutorial

Git Tutorial

Machine Learning Tutorial

DevOps Tutorial

B.Tech / MCA

DBMS tutorial

Data Structures tutorial

DAA tutorial

Operating System

Computer Network tutorial

Compiler Design tutorial

Computer Organization and Architecture

Discrete Mathematics Tutorial

Ethical Hacking

Computer Graphics Tutorial

Software Engineering

html tutorial

Cyber Security tutorial

Automata Tutorial

C Language tutorial

C++ tutorial

Java tutorial

.Net Framework tutorial

Python tutorial

List of Programs

Control Systems tutorial

Data Mining Tutorial

Data Warehouse Tutorial

Javatpoint Services

JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.

  • Website Designing
  • Website Development
  • Java Development
  • PHP Development
  • WordPress
  • Graphic Designing
  • Logo
  • Digital Marketing
  • On Page and Off Page SEO
  • PPC
  • Content Development
  • Corporate Training
  • Classroom and Online Training
  • Data Entry

Training For College Campus

JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week

Like/Subscribe us for latest updates or newsletter RSS Feed Subscribe to Get Email Alerts Facebook Page Twitter Page YouTube Blog Page

Источник

Оцените статью