Scanner throws FileNotFoundException but using bufferedreader and inputstream doesn't?
<p>I am trying to write code for a word guessing game, and it works well when I use bufferedreader and inputstream combined. But when I try it using scanner, it cannot find the file, even though in both instances the file is in the same folder. It is in a folder called res under the src folder in my project folder(I am coding in eclipse).</p>

<pre><code>import java.util.ArrayList;
import java.util.Scanner;
import java.io.File;

public class WordGen {

    private final String filename = "/res/words.txt";
    File file = new File(filename);
    Scanner input = null;
    private ArrayList&lt;String&gt; list = new ArrayList&lt;&gt;();

    public WordGen() {
        try {
            input = new Scanner(file);

            while (input.hasNextLine()) {
                String w = input.nextLine();
                list.add(w);
            }
        } catch (Exception ex) {
            System.out.println("File not found.");
        }
    }

    public String getword() {
        if (list.isEmpty()) {
            return "NOTHING";
        }
        return list.get((int) (Math.random() * list.size()));
    }
}

public class test {

    public static void main(String[] args) {
        WordGen wordgen = new WordGen();
        System.out.println(wordgen.getword());
    }

}
</code></pre>

<p>I tried searching for this problem but couldn't find it here. I am guessing it's  a very small error which I cannot figure out. Thanks and regards.</p>

<p>EDIT: Here's the other code that worked(Everything else same as before):</p>

<pre><code>public WordGenerator() 
 {
    try(InputStream input = getClass().getResourceAsStream(fileName);
            BufferedReader bfreader = new BufferedReader(new InputStreamReader(input))) 
    {
        String line = "";
        while ((line = bfreader.readLine()) != null)
            words.add(line);
    }
    catch (Exception e) 
    {
        System.out.println("Couldn't find file");
    }
}
</code></pre>