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

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

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

有帮助吗?

解决方案

初始化块就像任何代码位一样;它没有“附加”到它前面的任何字段/方法。要将值分配给字段,您必须显式使用该字段作为赋值语句的左侧。

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().

其他提示

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

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

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

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

尝试使用此方法只是从主

抛出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);
     }
  }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top