Java File: BufferedReader and FileReader

Handle files with BufferedReader and FileReader. Use readLine on text files.

Files. Ink splashes on the paper. It will persist for the lifetime of that sheet. Like the ink, a digital file has a lasting existence.

A key type for IO in Java is the BufferedReader. We use this to wrap around other types, such as FileReader. With BufferedReader, we can read lines, as strings, from a file.

Read lines. 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: We use a while-true loop to continually access the BufferedReader. When readLine returns null, no more data is available.

Line: The readLine method returns a String object. We can use this like any other String object in Java.

Close: Finally we invoke the close method. This is needed to clean up the system resources. Please note an IOException may be thrown.

Java program that uses BufferedReader import; import; import; 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(); } } Output First line. Second line.

Read lines into 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.ArrayList

Count lines: This program also counts the lines in a file. It accesses size() on the filled ArrayList.

Java program that uses BufferedReader, ArrayList import; import; import; 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); } } } Output Lines: 2 First line. Second line.

Count lines. It 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: When we call count() on the Stream, the file is read into memory line-by-line and the total count of lines is returned as a long.

Java program that counts lines in file import; import; import; 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(); } } Output Lines: 9

Throws exceptions. A file may not exist, or its data may be somehow invalid. An exception may be caused. We use the "throws IOException" modifier to make this risk clear.Exceptions

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.BufferedWriter

Copy files. 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.Files.Copy

Count letter frequencies. This example program uses a BufferedReader to read in lines from a file. It then hashes those lines' characters with HashMap and counts their frequencies.Letter Frequencies

A review. Many types are available in the packages. But BufferedStream, with its useful readLine method, is one of the easiest and most efficient types, at least for text files.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to