如何在 Java 中将文件读入 ArrayList?


Java 中,ArrayList 是一个可调整大小的数组,它是作为 Java 集合框架 的一部分实现的。它是一种动态数据结构,可以容纳任何类型的对象,包括基本类型,例如整数和字符。

根据问题陈述,我们必须将文件读入 ArrayList。首先,我们将获取文件的路径作为输入,然后我们将应用不同的方法来读取文件。

让我们开始吧!

示例

例如

假设输入文件为“myfile.txt”。

读取文件内容后,结果将是

文件内容为:[Hello everyone, I am a coder.]

将文件读入 ArrayList 的步骤

以下是将文件读入 Java 中的 ArrayList 的步骤

步骤 1:创建 ArrayList 的实例。

步骤 2:声明并初始化文件路径。

步骤 3:读取文件内容。

步骤 4:打印结果。

将文件读入 ArrayList 的方法

我们提供了不同方法的解决方案。

  • 使用 FileReader 和 BufferReader

  • 使用 Scanner

让我们逐一查看程序及其输出。

注意:此程序在任何 在线 Java 编译器 中都无法工作,因为它无法从在线编译器访问本地文件路径。

方法 1:使用 FileReader 和 BufferReader

在这种方法中,文件的路径由 FileReader 作为输入获取,然后使用 BufferReader 按行读取。

示例

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

public class Main
{
   //main method
   public static void main(String[] args) 
   {
      //instance of ArrayList
      ArrayList<String> lines = new ArrayList<>();
      //try & catch block to handle exception
      try {
         //taking the file path as input using FileReader
         FileReader fileReader = new FileReader("C:/Users/ Desktop/ 
Program/myfile.txt");
         
         //reading the content of the file using BufferedReader
         BufferedReader bufferedReader = new BufferedReader(fileReader);
         String line;
         
         //storing the content of the file in String
         while ((line = bufferedReader.readLine()) != null) {
            lines.add(line);
         }
         bufferedReader.close();
         fileReader.close();
      } catch (IOException e) {
         System.out.println("An error occurred while reading the file.");
         e.printStackTrace();
      }
      //print the result
      System.out.println("The contents of the file are: " + lines);
   }
}

输出

The contents of the file are: [Hello everyone, I am a coder.] 

方法 2:使用用户定义的方法

在这种方法中,文件的路径由 File 作为输入获取,然后使用 Scanner 按行读取。

示例

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

public class Main
{
   //main method
   public static void main(String[] args) 
   {
      //instance of ArrayList
      ArrayList<String> lines = new ArrayList<>();
      try 
      {
         //taking the file path as input using File
         File file = new File("example.txt");
         
         //reading the content of the file using Scanner
         Scanner scanner = new Scanner(file);
         while (scanner.hasNextLine()) {
            
            //storing the content of the file in String
            String line = scanner.nextLine();
            lines.add(line);
         }
      
         scanner.close();
      }
      catch (FileNotFoundException e)
      {
         System.out.println("An error occurred while reading the file.");
         e.printStackTrace();
      }
      
      //print the result
      System.out.println("The contents of the file are: " + lines);
   }
}

输出

The contents of the file are: [Hello Users, Let's code.]

在本文中,我们探讨了使用 Java 编程语言将文件读入 ArrayList 的不同方法。

另请参阅在 Java 中读取文件

更新于: 2024 年 8 月 6 日

3K+ 浏览量

启动您的 职业生涯

通过完成课程获得认证

立即开始
广告

© . All rights reserved.