2012-02-20 247 views
2

我想從一個文件中讀取輸入到一個Java applet中顯示爲吃豆人的水平,但我需要使用類似於getLine ()...所以我搜索了類似的東西,這是我發現的代碼:Java錯誤:默認的構造函數不能處理異常類型FileNotFound異常

File inFile = new File("textfile.txt"); 
FileInputStream fstream = new FileInputStream(inFile);//ERROR 
// Get the object of DataInputStream 
DataInputStream in = new DataInputStream(fstream); 
BufferedReader br = new BufferedReader(new InputStreamReader(in)); 

標誌着我行「ERROR」給我,說:「默認的構造無法處理的異常類型FileNotFoundException異常被拋出一個錯誤隱式超級構造函數,必須定義一個顯式構造函數。「

我已經搜索了這個錯誤信息,但是我發現的一切似乎與我的情況無關。

+1

你能否展示更多的代碼,我們不知道你上面發佈的內容。 – jbranchaud 2012-02-20 00:36:20

+0

看到這個相關文章:[雖然構造默認的構造函數不能處理異常:類型拋出隱式超級構造函數拋出異常](http://stackoverflow.com/questions/6772709/while-constructing-the-default-constructor-can-沒有手柄的異常型excep)。 – 2012-02-20 00:49:54

+0

你甚至沒有準確地引用錯誤信息。它確實告訴你到底是什麼問題,但是如果你沒有看到實際存在的問題,則不會。 – EJP 2016-11-29 23:50:22

回答

-1

我會說textfile.txt不存在或不在當前目錄中。

因此您正試圖在不存在的文件上創建FileInputStream。

嘗試做一個

System.out.println(inFile.getAbsolutePath() + " " + inFile.exists()); 
上的文件

,並確保它在你嘗試使用它之前確實存在。

+2

我認爲問題是編譯器錯誤(我懷疑上面的代碼被粘貼在構造函數中) - 不是運行時'文件不存在'錯誤。 – ptyx 2012-02-20 00:48:25

5

無論是在你的子類拋出FileNotFoundException聲明一個顯式構造:

public MySubClass() throws FileNotFoundException { 
} 

或環繞有try-catch塊在基類中的代碼,而不是拋出一個FileNotFoundException異常:

public MyBaseClass() { 
    FileInputStream fstream = null; 
    try { 
     File inFile = new File("textfile.txt"); 
     fstream = new FileInputStream(inFile); 
     // Get the object of DataInputStream 
     DataInputStream in = new DataInputStream(fstream); 
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     // Do something with the stream 
    } catch (FileNotFoundException ex) { 
     Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); 
    } finally { 
     try { 
      // If you don't need the stream open after the constructor 
      // else, remove that block but don't forget to close the 
      // stream after you are done with it 
      fstream.close(); 
     } catch (IOException ex) { 
      Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); 
     } 
    } 
} 

無關,但是由於您正在編寫Java小程序,請記住您需要sign it才能執行IO操作。

1

你需要用try和catch環繞你的代碼如下:

try { 
    File inFile = new File("textfile.txt"); 
    FileInputStream fstream = new FileInputStream(inFile);//ERROR 
} catch (FileNotFoundException fe){ 
    fe.printStackTrace(); 
} 
// Get the object of DataInputStream 
DataInputStream in = new DataInputStream(fstream); 
BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
+0

Adel,如果您像這樣編寫代碼,'DataStreamStream'構造函數不可見'fstream'。你需要重構你的代碼,並在try塊之外有一個null引用'fstream',就像我在我的例子中所做的那樣。另外,由於你沒有使用try/catch語句來包圍整個塊,所以在聲明'in'和'br'變量之前檢查'fstream!= null'並不是一個壞主意。 – 2012-02-20 01:03:11

+0

@AnthonyAccioly我並沒有提出一個完整的解決方案,我只是給你一個線索,try和catch應包圍可以產生提到的錯誤都行。可悲的是,我沒有檢查哪條線路。您可以在您方便的時候使用它:) – 2012-02-20 01:40:29

0

這是猜測,因爲我們沒有完整的代碼。

從的Javadoc: 公衆的FileInputStream(File file)在拋出FileNotFoundException異常

這意味着,當你做一個新的FileInputStream()像你這樣,就可以回來了FileNotFoundException異常。這是一個檢查的異常,您需要重新拋出(即在執行新操作的方法中添加'拋出FileNotFoundException')或捕獲(請參閱其他try/catch響應)。