如何在 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.