Read File Into an Array in Java


There are multiple ways to read a file into an array in Java. Here are three common methods:

Method 1: Using BufferedReader and ArrayList

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;

public class ReadFileIntoArray {
    public static void main(String[] args) {
        ArrayList<String> lines = new ArrayList<>();
        try {
            BufferedReader reader = new BufferedReader(new FileReader("filename.txt"));
            String line = reader.readLine();
            while (line != null) {
                lines.add(line);
                line = reader.readLine();
            }
            reader.close();
        } catch (Exception e) {
            System.err.format("Exception occurred trying to read '%s'.", "filename.txt");
            e.printStackTrace();
        }
        String[] linesArray = lines.toArray(new String[lines.size()]);
        // do something with the array
    }
}

This method uses a BufferedReader to read the file line by line and adds each line to an ArrayList. Once all lines have been read, the ArrayList is converted to an array using the toArray() method.

Method 2: Using Scanner and ArrayList

import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;

public class ReadFileIntoArray {
    public static void main(String[] args) {
        ArrayList<String> lines = new ArrayList<>();
        try {
            Scanner scanner = new Scanner(new File("filename.txt"));
            while (scanner.hasNextLine()) {
                lines.add(scanner.nextLine());
            }
            scanner.close();
        } catch (Exception e) {
            System.err.format("Exception occurred trying to read '%s'.", "filename.txt");
            e.printStackTrace();
        }
        String[] linesArray = lines.toArray(new String[lines.size()]);
        // do something with the array
    }
}

This method uses a Scanner to read the file line by line and adds each line to an ArrayList. Once all lines have been read, the ArrayList is converted to an array using the toArray() method.

Method 3: Using Files.readAllLines()

import java.nio.file.Files;
import java.nio.file.Paths;

public class ReadFileIntoArray {
    public static void main(String[] args) {
        try {
            String[] lines = Files.readAllLines(Paths.get("filename.txt")).toArray(new String[0]);
            // do something with the array
        } catch (Exception e) {
            System.err.format("Exception occurred trying to read '%s'.", "filename.txt");
            e.printStackTrace();
        }
    }
}

This method uses the readAllLines() method from the Files class to read all lines from the file into a List, which is then converted to an array using the toArray() method.



About the author

William Pham is the Admin and primary author of Howto-Code.com. With over 10 years of experience in programming. William Pham is fluent in several programming languages, including Python, PHP, JavaScript, Java, C++.