java的try和catch IOException异常问题

问题描述 投票:8回答:3

我想使用一些代码,我发现在this page的底部。下面是我为它创建了一个类的代码:

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

请告诉我的方式!在此先感谢您的帮助!

java try-catch ioexception
3个回答
4
投票

初始化块就像任何代码比特;它不是“附接”于前述的任何字段/方法。要指定值的字段,你必须明确地使用字段赋值语句的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
投票

你得到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.
   }
}

1
投票

countLines(String filename)方法抛出IOException异常。

你不能在成员声明中使用它。你需要在main(String[] args)方法进行操作。

main(String[] args)方法通过了countLines抛出它的IOException异常,它会需要处理或声明。

试试这个刚刚扔从主IOException异常

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

或者这处理,并在unchecked抛出: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);
     }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.