How to read text file and store in array in Java?

Reading data from a text file and storing it in an array is a common task in Java programming. The Java language provides several classes and methods that allow you to easily read from text files and load the contents into arrays for further processing. In this comprehensive guide, we will explore the various ways to read a text file and store the contents in an array in Java.

1. Using Scanner to read text file into array

One of the easiest ways to read a text file and store it in an array is by using the Scanner class. The Scanner class allows you to read data from different sources like files, input streams etc. Here is a simple code snippet to read a text file line by line and store each line in a String array:


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

public class ReadFileToArray {

  public static void main(String[] args) {
    
    String[] array = new String[100]; 
    int i = 0;

    try {
      Scanner sc = new Scanner(new File("data.txt"));
      
      while (sc.hasNextLine()) {
        array[i] = sc.nextLine();
        i++; 
      }
      
      sc.close();
  
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }  

}

In the above code, we first create an empty String array with initial capacity of 100 elements to store the lines. Then we create a Scanner object to read the text file “data.txt”.

Inside the while loop, we read each line using sc.nextLine() and store it in the array. The variable i is used as index to insert each subsequent line in the array.

Finally, we close the Scanner to prevent resource leak.

Key things to note:

  • Scanner’s nextLine() method returns the line as a String.
  • We need to handle FileNotFoundException in case file is not found.
  • Array size needs to be large enough to store all the lines. ArrayList can also be used.
  • Scanner is auto-closeable, so can be wrapped in try-with-resources block.

2. Using BufferedReader to read text file into array

Another class that can be used to read text files is BufferedReader. It reads text from a character-based input stream and buffers characters to provide efficient reading of characters, arrays and lines.

Here is how we can use BufferedReader to read a text file into a String array:


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class ReadFileToArrayBufferedReader {

  public static void main(String[] args) throws IOException {
      
    String[] array = new String[100]; 
    int i = 0;
  
    try (BufferedReader br = new BufferedReader(new FileReader("data.txt"))) {
      
      String line;
      while ((line = br.readLine()) != null) {
        array[i] = line;
        i++;
      }
      
    } catch (IOException e) {
      e.printStackTrace(); 
    }

  }

}  

In this approach we wrap the FileReader in a BufferedReader. We read each line using br.readLine() in the loop and store it in the array.

The readLine() method returns null when end of file is reached.

Key things to note:

  • BufferedReader reads text efficiently line by line.
  • We need to handle IOException.
  • BufferedReader supports try-with-resource to auto-close.
  • readLine() returns null at end of file.

3. Reading file into array using Files.readAllLines()

The java.nio.file.Files class provides convenient methods to read and write files. We can use Files.readAllLines() to read all lines of a file into a List.

  
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;

public class ReadFileToArrayNIO {

  public static void main(String[] args) throws IOException {

    List lines = Files.readAllLines(Paths.get("data.txt"));
    
    // Convert List to array
    String[] array = lines.toArray(new String[0]);

  } 

}

Files.readAllLines() reads the file into a List of strings with each element as a line. We then simply convert the List to an array using toArray().

Key things to note:

  • Reads all lines in one go into a List.
  • Need to handle IOException.
  • Easily convert List to array.
  • Preferred approach for small to medium sized files.

4. Read file and store lines in ArrayList

For a large file, it is better to store the lines in an ArrayList rather than a simple array. ArrayLists are dynamic in nature and we don’t need to worry about the size.

Here is how we can read lines from a text file into an ArrayList:



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

public class ReadFileToArrayList {

  public static void main(String[] args) {

    ArrayList lines = new ArrayList<>();
    
    try {
      Scanner sc = new Scanner(new File("data.txt")); 

      while (sc.hasNextLine()) {
        lines.add(sc.nextLine());
      }
      
      sc.close();
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

  }

}

Instead of reading into an array, we now create an ArrayList and use add() method to add each line to the list. Rest of the code remains same as our first example using Scanner.

The advantage is that we don’t need to worry about the size of the array or increasing it dynamically.

Key things to note:

  • ArrayList is dynamic so no need to worry about size.
  • New elements are added using add() method.
  • ArrayList can be converted to array if needed.
  • Better for large files compared to array.

5. Reading big file using Java 8 Stream

For very large files, we can leverage Java 8 Stream API to read the file and store contents.

Here is how we can read a large text file to Stream and collect contents to a List:


import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ReadLargeFileToList {

  public static void main(String[] args) throws IOException {

    try (Stream stream = Files.lines(Paths.get("largefile.txt"))) {
            
      List lines = stream.collect(Collectors.toList());
      
    } catch (IOException e) {
      e.printStackTrace();
    }

  }

}

Files.lines() returns a Stream that lazily loads and processes the lines on demand. We then collect the stream into a List using collect().

The key benefit is that the file contents are not loaded completely in memory at once. They are processed line by line on demand from the stream.

Key things to note:

  • Stream lazily loads file contents line by line.
  • No need to load everything in memory.
  • Collect stream to List, Set etc.
  • Ideal for large files with millions of lines.

6. Reading file into String array using Apache Commons IO

The Apache Commons IO library provides useful utility methods for working with files and I/O including file reading.

We can use IOUtils class to easily read all lines of a file into a String array:


import org.apache.commons.io.IOUtils;

import java.io.FileInputStream;
import java.io.IOException;

public class ReadFileToStringArrayCommonsIO {

  public static void main(String[] args) throws IOException {

    String[] lines = IOUtils.readLines(new FileInputStream("data.txt"), "UTF-8") 
      .toArray(new String[0]);

  }

}  

IOUtils.readLines() returns a List which we convert to String array. The charset is provided as second parameter.

Key things to note:

  • Simple one liner to read file into String array.
  • Specify charset as second parameter.
  • Returns List which can be converted to array.
  • Requires Apache Commons IO library.

7. Multi-dimensional array to store CSV file data

For reading CSV files, it is common to store contents in a multi-dimensional string array with each element representing a row and column value.

Let’s see an example code to read from a CSV file and store in multi-dimensional array:


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
  
public class ReadCSVToMultiArray {

  public static void main(String[] args) throws IOException {
  
    try (BufferedReader br = new BufferedReader(new FileReader("data.csv"))) {
	
      String line = br.readLine();
      int cols = line.split(",").length;
	
      String[][] data = new String[100][cols];
      int row = 0;
		
      while ((line = br.readLine()) != null) {
				
        String[] values = line.split(",");
				
        for(int i=0; i

Here we first read the header line to get number of columns in the CSV. We initialize the 2D array with rows and columns.

Then we split each line on comma to extract the values into a string array. We store each value in the corresponding row and column index in the 2D array.

Key things to note:

  • Initialize 2D array with number of rows and columns.
  • Split CSV line to get values in String array.
  • Store each value in row and column index.
  • Use fixed number of rows or ArrayList for rows.

Conclusion

In this guide, we explored various options to read data from text files and store in arrays and lists in Java.

The key options are:

  • Using Scanner and read each line into array
  • Read file line by line with BufferedReader
  • Leverage Files.readAllLines() to store in List
  • For large files use Java 8 Streams
  • Use Apache Commons IO for convenience
  • Store CSV data in multi-dimensional array

The best approach depends on the specific requirements like file size, memory usage, performance etc.

In summary, Java provides very flexible APIs to read file content into data structures like arrays and lists for processing.

Leave a Comment