So I have been stuck on this for a while. Basically, I have a file created called "word.txt" and whenever I run the program it gives me this error:
Exception in thread "main" java.io.FileNotFoundException: word.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at Hangman1.main(Hangman1.java:6)
Here's my code:
import java.io.File;
import java.util.*;
public class Hangman1 {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(new File("word.txt"));
String in = "";
in = input.nextLine();
}
}
Ironically, I created the file "word.txt" in the same directory as my java file and yet, it's still giving me that error.
Relative paths can be used, but they can be tricky. The best solution is to know where your files are being saved, that is, print the folder:
This code should print the folder where it is looking for. Place the file there and you'll be good to go.
Put the word.txt directly as a child of the project root folder and a peer of src
Disclaimer: I'd like to explain why this works for this particular case and why it may not work for others.
Why it works:
When you use
File
or any of the otherFileXxx
variants, you are looking for a file on the file system relative to the "working directory". The working directory, can be described as this:When you run from the command line
C:\EclipseWorkspace\ProjectRoot\bin > java com.mypackage.Hangman1
the working directory is
C:\EclipseWorkspace\ProjectRoot\bin
. With your IDE (at least all the ones I've worked with), the working directory is theProjectRoot
. So when the file is in theProjectRoot
, then using just the file name as the relative path is valid, because it is at the root of the working directory.Similarly, if this was your project structure
ProjectRoot\src\word.txt
, then the path"src/word.txt"
would be valid.Why it May not Work
For one, the working directory could always change. For instance, running the code from the command line like in the example above, the working directory is the
bin
. So in this case it will fail, as there is notbin\word.txt
Secondly, if you were to export this project into a jar, and the file was configured to be included in the jar, it would also fail, as the path will no longer be valid either.
That being said, you need to determine if the file is to be an embedded-resource (or just "resource" - terms which sometimes I'll use interchangeably). If so, then you will want to build the file into the classpath, and access it via an URL. First thing you would need to do (in this particular) case is make sure that the file get built into the classpath. With the file in the project root, you must configure the build to include the file. But if you put the file in the
src
or in some directory below, then the default build should put it into the class path.You can access classpath resource in a number of ways. You can make use of the
Class
class, which hasgetResourceXxx
method, from which you use to obtain classpath resources.For example, if you changed your project structure to
ProjectRoot\src\resources\word.txt
, you could use this:getResourceAsStream
returns anInputStream
, but obtains an URL under the hood. Alternatively, you could get anURL
if that's what you need.getResource()
will return an URLFor Maven users, where the directory structure is like
src/main/resources
, the contents of theresources
folder is put at the root of the classpath. So if you have a file in there, then you would only usegetResourceAsStream("/thefile.txt")
Make sure when you create a txt file you don't type in the name "name.txt", just type in "name". If you type "name.txt" Eclipse will see it as "name.txt.txt". This solved it for me. Also save the file in the src folder, not the folder were the .java resides, one folder up.
I was reading path from a properties file and didn't mention there was a space in the end. Make sure you don't have one.
Your file should directly be under the project folder, and not inside any other sub-folder.
If the folder of your project is named for e.g.
AProject
, it should be in the same place as yoursrc
folder.i think it always boils to the
classpath
. having said that if you run from the same folder where your .class is then changeScanner input = new Scanner(new File("word.txt"));
toScanner input = new Scanner(new File("./word.txt"));
that should work