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

3
  • You can either create it in net beans or give the complete path of your file Commented Sep 15, 2014 at 21:10
  • I posted my answer lemme know how much it helped you Commented 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 it Commented Sep 15, 2014 at 21:24

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

1 Comment

This worked though I got java.security.spec.InvalidKeySpecException: java.security.InvalidKeyException: invalid key format
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.

5 Comments

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 did you try my answer?
@user3624385: Just use "Other..." use type "Empty File" and add the extension manually to the file name.
@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 mine works and you still have some questions about fabian answer?
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.

4 Comments

@MadProgrammer did you get the links? are you going to report? May I ask where I mentioned Maven in my post? :)
"I create a txt file for my goal and reside it in main folder" sounds a lot like maven to me! but that's me. And yes
Hi Kick Buttowski! I'm trying the relative path solution. I'm not sure whether or not my question makes sense but I'll ask it anyway: "how do I put a file in the main folder?" Thanks again.
simple ,cut and paste it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.