java.io.FileNotFoundException:系统找不到指定的文件 [英] java.io.FileNotFoundException: the system cannot find the file specified

查看:367
本文介绍了java.io.FileNotFoundException:系统找不到指定的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直坚持这一点。基本上,我有一个名为word.txt创建的文件,每当我运行该程序,它给了我这个错误:

 异常在线程mainjava.io.FileNotFoundException:word.txt(系统找不到指定的文件)
在java.io.FileInputStream.open(本地方法)
在java.io.FileInputStream。 < init>(Unknown Source)
在java.util.Scanner。< init>(Unknown Source)
在Hangman1.main(Hangman1.java:6)


$ b $ p

这是我的代码:

  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();






$讽刺的是,我创建了文件word.txt在我的java文件相同的目录,但它仍然给我这个错误。

解决方案

直接把word.txt项目根目录文件夹的子项和src的同级

  Project_Root 
src
word.txt

免责声明:我想解释为什么这个特殊情况适用为什么它可能不适用于其他人。



为什么会这样:

当您使用 File 或任何其他 FileXxx 变体时,您正在文件系统上查找相对于工作目录。工作目录,可以这样描述:

当你从命令行运行时

C:\EclipseWorkspace\ProjectRoot\bin> java com.mypackage.Hangman1



工作目录是 C:\EclipseWorkspace\ProjectRoot \bin 。用你的IDE(至少我所有的工作),工作目录是 ProjectRoot 。因此,当文件位于 ProjectRoot 中时,只使用文件名作为相对路径是有效的,因为它位于工作目录的根目录。



同样,如果这是您的项目结构 ProjectRoot \src\word.txt ,那么路径src / word.txt会有效。



为什么它可能不起作用



其中之一,工作目录可以随时更改。例如,像上例那样从命令行运行代码,工作目录是 bin 。所以在这种情况下,它会失败,因为没有 bin \word.txt



其次,如果你将这个项目导出到一个jar文件,并配置文件被包括在jar中,它也会失败,因为路径将不再有效。

这就是说,你需要确定文件是否是一个嵌入式资源(或者只是资源 - 有时我会交替使用的术语)。如果是这样,那么你将要建立的文件到类路径,并通过URL访问它。你需要做的第一件事(在这个特定的情况下)是确保文件 build 到类路径中。使用项目根目录中的文件,您必须配置构建以包含文件。 如果将文件放在 src 或下面的某个目录中,那么默认的构建应该把它放到类路径中。

您可以通过多种方式访问​​classpath资源。您可以使用 Class 类,它具有 getResourceXxx 方法,您可以从中获取类路径资源。 例如,如果您将项目结构更改为 ProjectRoot \src\resources\word.txt ,则可以使用以下命令:

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

getResourceAsStream 返回一个 InputStream ,但是获得了一个URL。或者,如果这是你所需要的,你可以得到 URL getResource( ) 将返回一个URL



对于Maven用户,其目录结构如 src / main / resources resources 文件夹的内容放在classpath的根目录下。所以如果你有一个文件,那么你只能使用 getResourceAsStream(/ thefile.txt)


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.

解决方案

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 (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")

这篇关于java.io.FileNotFoundException:系统找不到指定的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆