Java 使用 BufferedReader 时如何抛出 FileNotFoundException?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20316153/
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
How to throw FileNotFoundException when using BufferedReader?
提问by user2826974
import java.util.ArrayList;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Random;
public class WordList {
private static Random r = new Random();
private static ArrayList<String> words = new ArrayList<String>();
String filename = "Cities.txt";
public static void main(String[] args) throws IOException {
WordList wl = new WordList();
buffR(wl.filename);
System.out.println(words);
System.out.println(getRandomWord());
}
public static ArrayList buffR(String filename) throws IOException {
words.clear();
String line = null;
BufferedReader br = null;
br = new BufferedReader(new FileReader(filename));
while ((line = br.readLine()) != null) {
words.add(line);
}
br.close();
return words;
}
public static String getRandomWord() {
WordList wl = new WordList();
String randomWord;
if (words.size() > 0) {
int index = r.nextInt(words.size());
randomWord = words.get(index);
} else {
randomWord = wl.filename + " is missing";
}
return randomWord;
}
}
There is a java.io.FileNotFoundException (regarding my BufferedReader method) when I run this code(I intentionally put a no existing file). However, I already threw IOException in my method header. What should I do to not encounter this error again?
当我运行此代码(我故意放置一个不存在的文件)时,有一个 java.io.FileNotFoundException(关于我的 BufferedReader 方法)。但是,我已经在我的方法头中抛出了 IOException。我应该怎么做才能不再遇到此错误?
采纳答案by Steve P.
All that adding a throws
does it say that the method can throw an Exception
, you want to be handling an Exception
if it is thrown. To do this, you can wrap the code chunk is a try-catch block. There is no way to stop an Exception
from being thrown if it should be thrown, but the try-catch block will make it so that your program doesn't crash. The Oracle Tutorialis quite helpful.
添加 a 的所有内容throws
都表明该方法可以抛出一个Exception
,Exception
如果它被抛出,您想要处理它。为此,您可以将代码块包装为一个 try-catch 块。Exception
如果它应该被抛出,就没有办法阻止它被抛出,但是 try-catch 块将使它不会让你的程序崩溃。在甲骨文教程是非常有帮助的。
For this example, you would do something like this:
对于此示例,您将执行以下操作:
try
{
//This already throws FileNotFoundException
br = new BufferedReader(new FileReader(filename));
}
catch(FileNotFoundException e)
{
e.printStackTrace();
}
回答by Elliott Frisch
How about this approach? I changed a few things, but I've documented it below.
这种方法怎么样?我改变了一些东西,但我在下面记录了它。
// static, pass in the initial words lits.
public static List<String> buffR(List<String> words,
String filename) throws IOException {
words.clear(); // If you want to reuse it.
String line = null;
BufferedReader br = null;
File file = null; // A file to read.
try {
file = new File(filename); // Create a File object.
if (! file.exists()) { // It's not there!
throw new FileNotFoundException("Could not find file: " + filename);
}
// Proceed as before...
br = new BufferedReader(new FileReader(file));
while ((line = br.readLine()) != null) {
words.add(line);
}
} finally {
br.close(); // Let's try to not leak any resources.
}
return words;
}