Java Try 和 Catch IOException 必须被捕获或声明为抛出
我正在尝试使用在 this 底部找到的一些代码页面。以下是我为其创建的类中的代码:
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 块,但这也不起作用(但我认为我做得不对)。这是我的 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);
}
}
请给我指路!
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);
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
初始化块就像任何代码位一样;它没有“附加”到它前面的任何字段/方法。要将值分配给字段,您必须显式使用该字段作为赋值语句的左侧。
另外,您的
countLines
可以变得更简单:根据我的测试,看起来您可以在
close()
之后getLineNumber()
。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.
Also, your
countLines
can be made simpler:Based on my test, it looks like you can
getLineNumber()
afterclose()
.您收到 IOException 的原因是因为您没有捕获 countLines 方法的 IOException。你会想做这样的事情:
The reason you are getting the the IOException is because you are not catching the IOException of your countLines method. You'll want to do something like this:
您的
countLines(String filename)
方法抛出 IOException。您不能在成员声明中使用它。您需要在
main(String[] args)
方法中执行该操作。您的 main(String[] args) 方法将获取 countLines 向其抛出的 IOException,并且需要处理或声明它。
尝试从 main 或 this 抛出 IOException 来
处理它并将其包装在未经检查的 IllegalArgumentException 中:
Your
countLines(String filename)
method throws IOException.You can't use it in a member declaration. You'll need to perform the operation in a
main(String[] args)
method.Your
main(String[] args)
method will get the IOException thrown to it by countLines and it will need to handle or declare it.Try this to just throw the IOException from main
or this to handle it and wrap it in an unchecked IllegalArgumentException: