Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.io.FileNotFoundException: the system cannot find the file specified

I have a file named "word.txt".

It is in the same directory as my java file.

But when I try to access it in the following code this file not found error occurs:

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();               } } 
like image 819
name123 Avatar asked Nov 09 '13 03:11

name123


People also ask

How do you fix the system Cannot find the file specified error?

Use SFC to fix system cannot finds the file specified error. In Command Prompt, type the following command: “sfc /scannow”. Now press Enter. After scanning and correcting errors, restart the computer and check if the “system cannot find the file specified” error is fixed.

What is Java IO FileNotFoundException?

java.io.FileNotFoundException. Signals that an attempt to open the file denoted by a specified pathname has failed. This exception will be thrown by the FileInputStream , FileOutputStream , and RandomAccessFile constructors when a file with the specified pathname does not exist.


1 Answers

Put the word.txt directly as a child of the project root folder and a peer of src

Project_Root     src     word.txt 

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 other FileXxx 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 the ProjectRoot. So when the file is in the ProjectRoot, 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 not bin\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 has getResourceXxx 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:

InputStream is = Hangman1.class.getResourceAsStream("/resources/word.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 

getResourceAsStream returns an InputStream, but obtains an URL under the hood. Alternatively, you could get an URL if that's what you need. getResource() will return an URL

For Maven users, where the directory structure is like src/main/resources, the contents of the resources folder is put at the root of the classpath. So if you have a file in there, then you would only use getResourceAsStream("/thefile.txt")

like image 59
Paul Samsotha Avatar answered Oct 13 '22 16:10

Paul Samsotha