How to throw FileNotFoundException when using BufferedReader? [closed]

好久不见. 提交于 2019-12-13 09:31:59

问题


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?


回答1:


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 Tutorial is quite helpful.

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



回答2:


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


来源:https://stackoverflow.com/questions/20316153/how-to-throw-filenotfoundexception-when-using-bufferedreader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!