I'm very new to Java. I want to read from a file, but I keep getting my "file not found" exception message. I learned that my .txt file had to be in the same directory as my Java program. On netbeans, how do I place my .txt file in the same directory as my Java program. Sorry this question is so basic, I'm in my first few weeks of java
-
You can either create it in net beans or give the complete path of your fileStackFlowed– StackFlowed2014-09-15 21:10:35 +00:00Commented Sep 15, 2014 at 21:10
-
I posted my answer lemme know how much it helped youKick Buttowski– Kick Buttowski2014-09-15 21:21:56 +00:00Commented Sep 15, 2014 at 21:21
-
When running with netbeans, current working directory is normally the project folder (where src lives), but this can be changed. At runtime, the working dir depends on a number factors, but you can use System.getProperty("user.dir") to find itMadProgrammer– MadProgrammer2014-09-15 21:24:42 +00:00Commented Sep 15, 2014 at 21:24
3 Answers
Create the text file and you have to put the file in the folder of project like in my case:
C:\Users\UsmanYaqoob\Documents\NetBeansProjects\BookQuestions
BookQuestions is my project
1 Comment
java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format- Go to the Files Tab (next to Projects and Services)
- In the context menu of the root folder of your project use New >
<approquiate type>to create a new file
Of course you can also use other means to move your file there, e.g. the file explorer of your OS.
However I'd recommend using the "working directory" option of the project configuration (you can reach this using the combobox at the top of the window) to use a directory you can reach easier using your OS file explorer.
5 Comments
There are two ways to accomplish what you are looking for
Absolute Path
like
"C:\\Users\\kick\\Documents\\NetBeansProjects\\ReadFile\\newfile.txt"
For the file separator read this:
With the Java libraries for dealing with files, you can safely use / (slash, not backslash) on all platforms. The library code handles translating things into platform-specific paths internally.
You might want to use File.separator in UI, however, because it's best to show people what will make sense in their OS, rather than what makes sense to Java.
Update: I have not been able, in five minutes of searching, to find the "you can always use a slash" behavior documented. Now, I'm sure I've seen it documented, but in the absense of finding an official reference (because my memory isn't perfect), I'd stick with using File.separator because you know that will work.
Relative Path
like
"newfile.txt"
Note: your file has to be located in project folder which is current working directory. In better sense, the folder that contains all of your related content
For example, if the name of your project is ReadFile, the file hast to be in folder that has the same name which ReadFile.
Note: Personally, I manually create a txt file for my goal and reside it in main folder.