Search code examples
javafileintellij-ideabufferedreaderfileinputstream

Buffered reader can't load file?


I'm working on a project for school, where I want to load questions from a file. However, Java is unable to load the file. I am sure the text file is named right and in the right direactory, but here they are:

game>src game>src game>out>production>game game>out>production>game

private static void init(){
    BufferedReader br = null;
    ArrayList<String> strings= new ArrayList<String>();
    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(new File("questions.txt")));

        while ((sCurrentLine = br.readLine()) != null) {
            strings.add(sCurrentLine);
        }

    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    int i = 0;
    while(i < strings.size()){
        String text = strings.get(i++);
        int numAnswers = Integer.parseInt(strings.get(i++));
        ArrayList<String> answers = new ArrayList<String>();
        for (int i2 = 0; i2 < numAnswers; i2++){
            answers.add(strings.get(i++));
        }
        questions.add(new Question(text, answers));
    }
}

Solution

  • Try scr/questions.txt as path.

    br = new BufferedReader(new FileReader(new File("src/questions.txt")));