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

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

问题描述

我有一个名为word.txt"的文件.

I have a file named "word.txt".

它与我的 java 文件在同一目录中.

It is in the same directory as my java file.

但是,当我尝试在以下代码中访问它时,会出现 file not found 错误:

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)

这是我的代码:

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直接作为项目根文件夹的子目录和src的同级

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.

为什么有效:

当您使用 File 或任何其他 FileXxx 变体时,您是在文件系统上寻找与 工作目录"相关的文件.工作目录,可以这样描述:

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:

当你从命令行运行时

C:EclipseWorkspaceProjectRootin >java com.mypackage.Hangman1

工作目录是C:EclipseWorkspaceProjectRootin.对于您的 IDE(至少是我使用过的所有 IDE),工作目录是 ProjectRoot.因此,当文件位于 ProjectRoot 中时,仅使用文件名作为相对路径是有效的,因为它位于工作目录的根目录.

the working directory is C:EclipseWorkspaceProjectRootin. 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.

同样,如果这是您的项目结构 ProjectRootsrcword.txt,则路径 "src/word.txt" 将是有效的.

Similarly, if this was your project structure ProjectRootsrcword.txt, then the path "src/word.txt" would be valid.

为什么它可能不起作用

一方面,工作目录总是会改变.例如,像上面的例子一样从命令行运行代码,工作目录是 bin.所以在这种情况下它会失败,因为没有 binword.txt

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 binword.txt

其次,如果您将此项目导出到 jar 中,并且将文件配置为包含在 jar 中,它也会失败,因为路径也不再有效.

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.

话虽如此,您需要确定文件是否为 (或只是资源"——有时我会互换使用的术语).如果是这样,那么您需要将文件构建到类路径中,并通过 URL 访问它.您需要做的第一件事(在这种特殊情况下)是确保文件构建到类路径中.使用项目根目录中的文件,您必须配置构建以包含该文件.但是如果你把文件放在 src 或下面的某个目录中,那么默认构建应该把它放在类路径中.

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.

您可以通过多种方式访问​​类路径资源.您可以使用 Class类,它具有 getResourceXxx 方法,您可以从中获取类路径资源.

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.

例如,如果您将项目结构更改为 ProjectRootsrc esourcesword.txt,则可以使用:

For example, if you changed your project structure to ProjectRootsrc esourcesword.txt, you could use this:

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

getResourceAsStream 返回一个 InputStream,但在幕后获得一个 URL.或者,如果您需要,您可以获得 URL.getResource() 将返回一个 URL

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

对于 Maven 用户,目录结构类似于 src/main/resourcesresources 文件夹的内容放在类路径的根目录下.所以如果你有一个文件,那么你只会使用 getResourceAsStream("/thefile.txt")

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天全站免登陆