2013-11-26 94 views
0

我想讀取文件並將其存儲到私人類arraylist但是我得到此編譯器錯誤:WordList.java:8:未報告的異常java。 io.IOException;必須被捕或宣佈被拋出試圖讀取.txt文件並將其存儲到私人arraylist

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

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

+1

不利用方法名 – user2573153

回答

0

添加throws IOExceptionmain

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

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

您在方法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(); 
} 
} 
+0

是什麼拋出和捕獲嗎?我從來沒有理解捕捉的目的,但是當我使用投擲時,它似乎告訴系統會有錯誤,但忽略它。 @sambaheerathan – Ungeheuer

相關問題