long龙8国际中文网

一个关于 编程问题的解答网站.

有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java正在尝试读取。txt文件,并将其存储到专用arraylist中

我试图读取一个文件并将其存储到一个私有类arraylist中,但是我遇到了这个编译器错误:WordList。java:8:未报告的异常java。伊奥。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();
  }
}

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


共 (3) 个答案

  1. # 1 楼答案

    您正在方法ArrayListConstructor(字符串文件名)中引发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();
     }
    }
    
  2. # 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. # 3 楼答案

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