1

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

user3624385
  • 51
  • 2
  • 2
  • 6
  • You can either create it in net beans or give the complete path of your file – StackFlowed Sep 15 '14 at 21:10
  • I posted my answer lemme know how much it helped you – Kick Buttowski Sep 15 '14 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 it – MadProgrammer Sep 15 '14 at 21:24

3 Answers3

2

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

Usman Yaqoob
  • 535
  • 5
  • 13
  • This worked though I got `java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format` – bademba Aug 14 '19 at 13:23
0

There are two ways to accomplish what you are looking for

  1. 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.

Source of the quoted answer

  1. 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.

Community
  • 1
  • 1
Kick Buttowski
  • 6,709
  • 13
  • 37
  • 58
0
  1. Go to the Files Tab (next to Projects and Services)
  2. 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.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • Thanks! Your advice almost got me there. I just couldn't find a New ".txt" Maybe I'm not seeing something, but I couldn't get a text file. Still very good advice though! – user3624385 Sep 15 '14 at 21:30
  • @user3624385 did you try my answer? – Kick Buttowski Sep 15 '14 at 21:31
  • @user3624385: Just use "Other..." use type "Empty File" and add the extension manually to the file name. – fabian Sep 15 '14 at 21:33
  • @fabian You solution is working well. To manually add the extension can I just do "rename file" and change the name to anythingcangohere.txt? Will that automatically make it a text file? Thanks again – user3624385 Sep 15 '14 at 22:09
  • @user3624385 mine works and you still have some questions about fabian answer? – Kick Buttowski Sep 15 '14 at 23:16