Netbeans - reading data file in src folder

I have a scanner that is trying to read a file called info.data in the src folder. I get an Exception in the stream "main" java.io.FileNotFoundException: info.data (the system cannot find the specified file). What address should I put in the scanner?

+7
source share
3 answers

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

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

In netbeans, the src folder is not the destination of the compiled classes, so if you use a relative path, the location that your program launches will not be the src folder.

This means that you usually have to β€œexpand” your assembly in order to copy the frantic file into the build path if you want it to work the way you mean. Many files are already copied to the build path (for example, property files), but if you include a data file that does not have a rule to be placed in the build path, you need to add the rule yourself.

+3
source

Try entering a path to it.

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

All Articles