Jake
Jake

Reputation: 71

Netbeans - Reading a data file in src folder

I have a scanner that's trying to read a file named info.data in the src folder.I get Exception in thread "main" java.io.FileNotFoundException: info.data (The system cannot find the file specified). What's the address I should put in the scanner?

Upvotes: 7

Views: 24842

Answers (3)

user330315
user330315

Reputation:

If the input file is always part of your application (i.e. you also put this into the .jar file later) you should use getResourceAsStream() in order to read its contents.

InputStream in = getClass().getResourceAsStream(filename);
Scanner scanner = new Scanner(in);

Upvotes: 4

Edwin Buck
Edwin Buck

Reputation: 70909

In netbeans, the src folder isn't the destination of the compiled classes, so if you are using a relative path, the location your program launches is not going to be the src folder.

That means you typically should "extend" your build to copy a non-source file into the build path if you want it to operate in the manner you imply. Many files already copy over to the build path (like properties files), but if you are including a data file that doesn't have a rule for being place in the build path, you need to add the rule yourself.

Upvotes: 3

corsiKa
corsiKa

Reputation: 82559

Try putting the path to it.

File f = new File("C:\\path\\src\\info.data");

Upvotes: 0

Related Questions