Home
Java
File Handling
This page was last reviewed on Jan 24, 2024.
Dot Net Perls
File. In Java we can read lines, as strings, from a file with BufferedReader. We use this to wrap around other types, such as FileReader.
Some methods. On BufferedReader, we can call readLine() to get a line. These methods are optimized, and will perform well for most Java programs.
Files.size
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.
Start We use a while-true loop to continually access the BufferedReader. When readLine returns null, no more data is available.
while
Next The readLine method returns a String object. We can use this like any other String object in Java.
Finally We invoke the close method. This is needed to clean up the system resources. Please note an 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.
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
Also This program counts the lines in a file. It accesses 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 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.
Info 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.
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.
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
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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Jan 24, 2024 (edit link).
Home
Changes
© 2007-2024 Sam Allen.