簡體   English   中英

嘗試讀取.txt文件並將其存儲到私有arraylist中

[英]Trying to read a .txt file and store it into a private arraylist

我正在嘗試讀取文件並將其存儲到私有類arraylist中,但是卻收到此編譯器錯誤:WordList.java:8:未報告的異常java.io.IOException; 必須被抓住或宣布被拋出

import java.util.*;
import java.io.*;
public class WordList
{
  private ArrayList<String> words = new ArrayList<String>();
  public void main(String[] args)
  {
    ArrayListConstructor("Cities.txt");
    System.out.println(words);
  }
  public void ArrayListConstructor(String filename) throws IOException
  {
    BufferedReader br = null;
    br = new BufferedReader(new FileReader(filename));
    String line = br.readLine();
    while (line != null)
    {
      this.words.add(line);
      line = br.readLine();
    }
    br.close();
  }
}

任何幫助將不勝感激。 謝謝。

main添加throws IOException

public void main(String[] args) throws IOException

或將方法包裝在try/catch塊中

public void main(String[] args)
{
    try{
       ArrayListConstructor("Cities.txt");
    }
    catch(IOException ex){
        ex.printStackTrace();
    }
    System.out.println(words);

 }
package testing;

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

public class WordList
{
    private static ArrayList<String> words = new ArrayList<String>();
    public static void main(String[] args) throws IOException
    {
        arrayListConstructor("Cities.txt");
        System.out.println(words);
    }
    public static void arrayListConstructor(String filename) throws IOException
    {
        BufferedReader br = null;
        br = new BufferedReader(new FileReader(filename));
        String line = br.readLine();
        while (line != null)
        {
            words.add(line);
            line = br.readLine();
        }
        br.close();
    }
}

您在ArrayListConstructor(String filename)方法中引發IOException異常,因此,每當使用此方法時,都應捕獲此異常

import java.io.*;
import java.util.*;

public class WordList {

private ArrayList<String> words = new ArrayList<String>();

public void main(String[] args) {
    try {
        ArrayListConstructor("Cities.txt");
    } catch (IOException ex) {
        System.out.println("Exception occured");
    }
    System.out.println(words);
}

public void ArrayListConstructor(String filename) throws IOException {
    BufferedReader br = null;
    br = new BufferedReader(new FileReader(filename));
    String line = br.readLine();
    while (line != null) {
        this.words.add(line);
        line = br.readLine();
    }
    br.close();
 }
}

或者,您可以再次引發此異常

import java.io.*;
import java.util.*;

public class WordList {

private ArrayList<String> words = new ArrayList<String>();

public void main(String[] args) throws IOException {
    ArrayListConstructor("Cities.txt");
    System.out.println(words);
}

public void ArrayListConstructor(String filename) throws IOException {
    BufferedReader br = null;
    br = new BufferedReader(new FileReader(filename));
    String line = br.readLine();
    while (line != null) {
        this.words.add(line);
        line = br.readLine();
    }
    br.close();
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM