【问题标题】:Trying to read a .txt file and store it into a private arraylist尝试读取 .txt 文件并将其存储到私有数组列表中
【发布时间】:2013-11-26 00:27:29
【问题描述】:

我正在尝试读取一个文件并将其存储到一个私有类数组列表中,但是我收到了这个编译器错误:WordList.java:8: unreported exception 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();
  }
}

任何帮助将不胜感激。谢谢。

【问题讨论】:

  • 方法名不要大写

标签: java arraylist bufferedreader


【解决方案1】:
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();
    }
}

【讨论】:

    【解决方案2】:

    throws IOException 添加到main

    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);
    
     }
    

    【讨论】:

      【解决方案3】:

      你在方法 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();
       }
      }
      

      【讨论】:

      • 投掷和接球有什么作用?一直不明白 catch 的目的,但是当我使用 throwing 时,它似乎告诉系统会有错误,但忽略它。 @sambaheerathan
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 2012-11-22
      • 2015-07-23
      • 2013-03-24
      相关资源
      最近更新 更多