2010-03-07 69 views
6

我正嘗試使用我在this page的底部找到的一小段代碼。下面是我爲它創建了一個類的代碼:Java嘗試並捕獲IOException異常問題

import java.io.LineNumberReader; 
import java.io.FileReader; 
import java.io.IOException; 

public class LineCounter { 
    public static int countLines(String filename) throws IOException { 
    LineNumberReader reader = new LineNumberReader(new FileReader(filename)); 
    int cnt = 0; 
    String lineRead = ""; 
    while ((lineRead = reader.readLine()) != null) {} 
    cnt = reader.getLineNumber(); 
    reader.close(); 
    return cnt; 
    } 
} 

我的目標是計算文本文件的行,這個數字存儲爲一個整數,然後用我的主類是整數。在我的主要課程中,我嘗試了幾種不同的方式來實現這一點,但是(作爲一名新程序員)我錯過了一些東西。下面是我想的第一件事:

String sFileName = "MyTextFile.txt"; 
private int lineCount = LineCounter.countLines(sFileName); 

有了這次嘗試,我得到的錯誤「未報告的異常java.io.IOException異常;必須捕獲或聲明被拋出。」我不明白爲什麼我得到這個,因爲我可以看到異常是在我的「countLines」方法中聲明的。我嘗試在我發佈的最後一段代碼下面使用try catch塊,但這也不起作用(我認爲我沒有做到這一點)。這裏是我嘗試趕上嘗試:

String sFileName = "MyTextFile.txt"; 
private int lineCount;{ 
    try{ 
     LineCounter.countLines(sFileName); 
    } 
    catch(IOException ex){ 
     System.out.println (ex.toString()); 
     System.out.println("Could not find file " + sFileName); 
    } 
} 

請告訴我的方式!在此先感謝您的幫助!

回答

3

初始化塊就像任何代碼比特;它沒有「附加」到任何前面的字段/方法。要爲字段賦值,你必須明確地使用該字段作爲賦值語句的lhs。

private int lineCount; { 
    try{ 
     lineCount = LineCounter.countLines(sFileName); 
     /*^^^^^^^*/ 
    } 
    catch(IOException ex){ 
     System.out.println (ex.toString()); 
     System.out.println("Could not find file " + sFileName); 
    } 
} 

此外,您可以countLines更簡單:根據我的測試

public static int countLines(String filename) throws IOException { 
    LineNumberReader reader = new LineNumberReader(new FileReader(filename)); 
    while (reader.readLine() != null) {} 
    reader.close(); 
    return reader.getLineNumber(); 
    } 

,它看起來像你getLineNumber()後可以close()

+1

答案在技術上是正確的(OP簡單地忘記將值賦給引用),但那不是一個** static **初始化塊。這只是一個初始化塊:) – BalusC 2010-03-07 20:15:56

+0

你的第一個代碼的作品完美!我太親近了:)你的第二個代碼也可以工作,但是NetBeans告訴我while循環是空的。該程序仍編譯並運行正常,所以我想這是可以接受的? – ubiquibacon 2010-03-07 20:16:59

+0

這完全忽略了內容。你實際上只對線的數量感興趣。你也可以使用'BufferedReader'和'int count'結合在循環內增加。 – BalusC 2010-03-07 20:18:02

1

您的countLines(String filename)方法拋出IOException。

您不能在成員聲明中使用它。您需要執行main(String[] args)方法中的操作。

您的main(String[] args)方法將得到countLines拋出的IOException,它將需要處理或聲明它。

嘗試使用此方法只從主

public class MyClass { 
    private int lineCount; 
    public static void main(String[] args) throws IOException { 
    lineCount = LineCounter.countLines(sFileName); 
    } 
} 

拋出IOException異常或該處理它,並在一個未檢查拋出:IllegalArgumentException它包:

public class MyClass { 
    private int lineCount; 
    private String sFileName = "myfile"; 
    public static void main(String[] args) throws IOException { 
    try { 
     lineCount = LineCounter.countLines(sFileName); 
    } catch (IOException e) { 
     throw new IllegalArgumentException("Unable to load " + sFileName, e); 
    } 
    } 
} 
+0

對不起,誤解了問題! – Brabster 2010-03-07 20:10:13

+0

謝謝你的回覆,你們都很棒! – ubiquibacon 2010-03-07 20:24:16

1

您得到IOException的原因是因爲您沒有捕獲countLines方法的IOException。你會想要做這樣的事情:

public static void main(String[] args) { 
    int lines = 0; 

    // TODO - Need to get the filename to populate sFileName. Could 
    // come from the command line arguments. 

    try { 
     lines = LineCounter.countLines(sFileName); 
    } 
    catch(IOException ex){ 
     System.out.println (ex.toString()); 
     System.out.println("Could not find file " + sFileName); 
    } 

    if(lines > 0) { 
    // Do rest of program. 
    } 
} 
+0

感謝您的回覆! – ubiquibacon 2010-03-07 20:24:47