java.io.FileNotFoundException: (No such file or directory) when running from eclipse

user2644085 picture user2644085 · Sep 10, 2013 · Viewed 55.7k times · Source

I am writing to a file and want console output,

// TODO Create a game engine and call the runGame() method
public static void main(String[] args) throws Exception { 
    NewGame myGame = new TheGame().new NewGame();
    myGame.runGame();
    PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
    System.setOut(out);
}

This gives me console output, but it throws the following exception:

java.io.FileNotFoundException: TheGame.txt (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at game.main(TheGame.java:512)

The file does exist.

Answer

Menelaos picture Menelaos · Sep 10, 2013

The file should be in contained within the root of your project.

When you execute a project in eclipse, the working directory is the most top level of your project.

Right click your project, click New>File, and make a txt file called "TheGame.txt".