9

我正在尝试使用在此页面底部找到的一些代码。这是我为它创建的类中的代码:

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

请给我指路!在此先感谢您的帮助!

4

3 回答 3

6

初始化程序块就像任何代码位一样;它没有“附加”到它之前的任何字段/方法。要将值分配给字段,您必须明确使用该字段作为赋值语句的 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().

于 2010-03-07T20:05:29.050 回答
1

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

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

您的main(String[] args)方法将获得 countLines 向其抛出的 IOException,并且需要对其进行处理或声明。

试试这个只是从 main 抛出 IOException

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

或者这个来处理它并将它包装在一个未经检查的 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);
     }
  }
}
于 2010-03-07T20:02:35.590 回答
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.
   }
}
于 2010-03-07T20:06:39.090 回答