Home > Java > javaTutorial > How to Read a File into an ArrayList in Java?

How to Read a File into an ArrayList in Java?

Barbara Streisand
Release: 2024-11-25 22:13:10
Original
596 people have browsed it

How to Read a File into an ArrayList in Java?

Reading a File into an ArrayList in Java

When working with text data, it's often useful to store it in a data structure for easy manipulation and processing. In Java, an ArrayList is a commonly used collection for holding a list of objects, including strings. This article demonstrates how to read the contents of a file into an ArrayList of strings.

File Preparation

First, ensure you have a text file with line-separated data, as shown in the provided example:

cat
house
dog
...
Copy after login

Java Code to Read the File

To read the contents of the file into an ArrayList of strings, use the following code:

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

public class FileToList {
    public static void main(String[] args) {
        try {
            // Create a Scanner object for the file
            Scanner s = new Scanner(new File("filepath"));

            // Create an ArrayList to store the words
            ArrayList<String> list = new ArrayList<>();

            // Loop through the file and add each word to the ArrayList
            while (s.hasNext()) {
                list.add(s.next());
            }

            // Close the Scanner
            s.close();

            // Print the ArrayList to the console
            System.out.println(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Copy after login

Explanation

  1. The code instantiates a Scanner object to read the file.
  2. It creates an empty ArrayList to store the strings.
  3. A while loop is used to iterate through each word in the file.
  4. The s.next() method retrieves the next word and adds it to the ArrayList.
  5. After reading the entire file, the Scanner is closed.
  6. The ArrayList is printed to the console for inspection.

Alternative for Reading Line by Line

If you prefer to read the file line by line instead of word by word, modify the code as follows:

...
// Loop through the file and add each line to the ArrayList
while (s.hasNextLine()) {
    list.add(s.nextLine());
}
...
Copy after login

In this case, s.hasNextLine() checks for the presence of a new line, and s.nextLine() retrieves the entire line.

The above is the detailed content of How to Read a File into an ArrayList in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template