Java尝试并捕获IOException异常问题
我正尝试使用我在this page的底部找到的一小段代码。下面是我为它创建了一个类的代码:Java尝试并捕获IOException异常问题
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);
}
}
请告诉我的方式!在此先感谢您的帮助!
初始化块就像任何代码比特;它没有“附加”到任何前面的字段/方法。要为字段赋值,你必须明确地使用该字段作为赋值语句的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()
。
您的countLines(String filename)
方法抛出IOException。
您不能在成员声明中使用它。您需要执行main(String[] args)
方法中的操作。
您的main(String[] args)
方法将得到countLines抛出的IOException,它将需要处理或声明它。
尝试使用此方法只从主
public class MyClass {
private int lineCount;
public static void main(String[] args) throws IOException {
lineCount = LineCounter.countLines(sFileName);
}
}
抛出IOException异常或该处理它,并在一个未检查抛出: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);
}
}
}
对不起,误解了问题! – Brabster 2010-03-07 20:10:13
谢谢你的回复,你们都很棒! – ubiquibacon 2010-03-07 20:24:16
您得到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.
}
}
感谢您的回复! – ubiquibacon 2010-03-07 20:24:47
答案在技术上是正确的(OP简单地忘记将值赋给引用),但那不是一个** static **初始化块。这只是一个初始化块:) – BalusC 2010-03-07 20:15:56
你的第一个代码的作品完美!我太亲近了:)你的第二个代码也可以工作,但是NetBeans告诉我while循环是空的。该程序仍编译并运行正常,所以我想这是可以接受的? – ubiquibacon 2010-03-07 20:16:59
这完全忽略了内容。你实际上只对线的数量感兴趣。你也可以使用'BufferedReader'和'int count'结合在循环内增加。 – BalusC 2010-03-07 20:18:02