C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
But unlike objects in memory, we must use special approaches, such as FileReader and loops, to use file data.
BufferedReader. 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.
Based on: Java 7 Java program that uses BufferedReader 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(); } } 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.
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 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); } } } 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 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(); } } 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.
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.
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.
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.
Types. 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.