Java尝试并捕获IOException问题 [英] Java Try and Catch IOException Problem

查看:74
本文介绍了Java尝试并捕获IOException问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用在

I am trying to use a bit of code I found at the bottom of this page. Here is the code in a class that I created for it:

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

我的目标是计算文本文件的行数,将该数字存储为整数,然后在我的主类中使用该整数.在主类中,我尝试了几种不同的制作方法发生这种情况,但是(作为一名新程序员)我错过了一些东西.这是我尝试的第一件事:

My objective is to count the lines of a text file, store that number as an integer, then use that integer in my main class. In my main class I tried a few different ways of making this happen, but (being a new programmer) I am missing something. Here is the first thing I tried:

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

通过这种尝试,我得到错误未报告的异常java.io.IOException;必须被捕获或声明为抛出".我不明白为什么要得到这个,因为如我所见,异常是在我的"countLines"方法中声明的.我试图在发布的最后代码下使用try catch块,但这还是行不通的(尽管我认为我做的不对).这是我的尝试捕获尝试:

With this attempt I get the error "unreported exception java.io.IOException; must be caught or declared to be thrown." I don't understand why I am getting this because as I can see the exception is declared in my "countLines" method. I tried to use a try catch block right under that last bit of code I posted, but that didn't work either (I don't think I did it right though). Here is my try catch attempt:

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

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

Please show me the way! Thanks in advance for your help!

推荐答案

Initializer块就像任何代码段一样;它没有附加"到它前面的任何字段/方法.要为字段分配值,您必须显式使用字段作为赋值语句的lhs.

Initializer block is just like any bits of code; it's not "attached" to any field/method preceding it. To assign values to fields, you have to explicitly use the field as the lhs of an assignment statement.

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 可以变得更简单:

Also, your countLines can be made simpler:

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

根据我的测试,看来您可以在 close()之后 getLineNumber().

Based on my test, it looks like you can getLineNumber() after close().

这篇关于Java尝试并捕获IOException问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆