Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Netbeans - Reading a data file in src folder

Tags:

java

netbeans

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?

like image 561
Jake Avatar asked Feb 21 '11 22:02

Jake


People also ask

What is src directory in Java?

When you create an API project, it is set up as a Java project with separate folders for source and class files. The source folder is named src . It contains the Java code of the application. A few Java classes are also created together with the new project.

How add data file in NetBeans?

Right-click on the main project folder, then select "New," then "Other." Select "Other" from the file selection window, then select "Folder." Give your folder a name and click "Finish."

Where does NetBeans store files?

userdir is the directory where NetBeans stores user configuration data such as window layouts, editor settings, menu and toolbar customizations and various module settings. Per default, it is a (hidden) directory called . netbeans stored in the user's home directory.

How do I open a directory in NetBeans?

To open a local file/folder (as a file-explorer) in Netbeans, in the top menu-bar goto: Window -> Favourites (or press Ctrl+3), this will open 'Favourites' pane, here you can open files or folders (in Linux, by default you will see your 'home' directory).


1 Answers

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);
like image 108
a_horse_with_no_name Avatar answered Oct 12 '22 15:10

a_horse_with_no_name