In Java we can read lines, as strings, from a file with BufferedReader
. We use this to wrap around other types, such as FileReader
.
On BufferedReader
, we can call readLine()
to get a line. These methods are optimized, and will perform well for most Java programs.
We put these objects together into a program. We specify a file on the disk—you will want to change this before running this program. Make a text file that has some lines.
while-true
loop to continually access the BufferedReader
. When readLine
returns null
, no more data is available.readLine
method returns a String
object. We can use this like any other String
object in Java.IOException
may be thrown.import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Program { public static void main(String[] args) throws IOException { // Create a BufferedReader from a FileReader. BufferedReader reader = new BufferedReader(new FileReader( "C:\\file.txt")); // Loop over lines in the file and print them. while (true) { String line = reader.readLine(); if (line == null) { break; } System.out.println(line); } // Close the BufferedReader. reader.close(); } }First line. Second line.
ArrayList
We often use an ArrayList
to store many strings. We can add lines from a text file to an ArrayList
. We call add()
when BufferedReader
returns a line in our loop.
size()
on the filled ArrayList
.import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; public class Program { public static void main(String[] args) throws IOException { ArrayList<String> list = new ArrayList<>(); // New BufferedReader. BufferedReader reader = new BufferedReader(new FileReader( "C:\\file.txt")); // Add all lines from file to ArrayList. while (true) { String line = reader.readLine(); if (line == null) { break; } list.add(line); } // Close it. reader.close(); // Print size of ArrayList. System.out.println("Lines: " + list.size()); // Print each line. for (String line : list) { System.out.println(line); } } }Lines: 2 First line. Second line.
Count
linesIt is possible to count the lines in a file with BufferedReader
and its lines()
Stream
. Lines()
returns a Stream
of Strings. It is lazily evaluated.
count()
on the Stream
, the file is read into memory line-by-line and the total count of lines is returned as a long.import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class Program { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new FileReader( "C:\\programs\\file.txt")); // Count lines in the file. // ... Call count() on the lines Stream. long lines = reader.lines().count(); System.out.println("Lines: " + lines); // Close it. reader.close(); } }Lines: 9
BufferedWriter
This type writes Strings to a file. It also handles chars, ranges of characters (like substrings) and ints. We write a String
array to lines in a file.
With the clearly-named Files.copy
method, we copy a file's contents from one location to another. We use a CopyOption
to improve our programs.
Many types are available in the java.io
packages. But BufferedStream
, with its useful readLine
method, is one of the easiest and most efficient types, at least for text files.