Java Try and Catch IOException 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2397714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Try and Catch IOException Problem
提问by ubiquibacon
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:
通过这次尝试,我得到了错误“未报告的异常 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);
}
}
Please show me the way! Thanks in advance for your help!
请给我指路!在此先感谢您的帮助!
采纳答案by polygenelubricants
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.
初始化块就像任何代码位一样;它没有“附加”到它之前的任何字段/方法。要将值分配给字段,您必须明确使用该字段作为赋值语句的 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);
}
}
Also, your countLines
can be made simpler:
此外,您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();
}
Based on my test, it looks like you can getLineNumber()
after close()
.
根据我的测试,看起来你可以getLineNumber()
在close()
.
回答by brabster
Your countLines(String filename)
method throws IOException.
您的countLines(String filename)
方法抛出 IOException。
You can't use it in a member declaration. You'll need to perform the operation in a main(String[] args)
method.
您不能在成员声明中使用它。您需要在main(String[] args)
方法中执行操作。
Your main(String[] args)
method will get the IOException thrown to it by countLines and it will need to handle or declare it.
您的main(String[] args)
方法将获得 countLines 向其抛出的 IOException,它需要处理或声明它。
Try this to just throw the IOException from main
试试这个只是从 main 抛出 IOException
public class MyClass {
private int lineCount;
public static void main(String[] args) throws IOException {
lineCount = LineCounter.countLines(sFileName);
}
}
or this to handle it and wrap it in an unchecked IllegalArgumentException:
或者这个来处理它并将它包装在一个未经检查的 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);
}
}
}
回答by JasCav
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:
您获得 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.
}
}