为了账号安全,请及时绑定邮箱和手机立即绑定

FileNotFoundException:系统找不到指定的文件

FileNotFoundException:系统找不到指定的文件

叮当猫咪 2019-11-05 12:04:44
FileNotFoundException:系统找不到指定的文件我有一份名为“word.txt".它位于与我的目录相同的目录中。java档案。但是,当我试图在下面的代码中访问它时,找不到文件错误发生: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();               }}
查看完整描述

3 回答

?
LEATH

TA贡献1936条经验 获得超6个赞

可以使用相对路径,但它们可能很棘手。最好的解决方案是知道您的文件保存在哪里,即打印文件夹:

import java.io.File;import java.util.*;public class Hangman1 {

    public static void main(String[] args) throws Exception {
        File myFile = new File("word.txt");
        System.out.println("Attempting to read from file in: "+myFile.getCanonicalPath());

        Scanner input = new Scanner(myFile);
        String in = "";
        in = input.nextLine();
    }}

此代码应打印它要查找的文件夹。把文件放在那里,你就可以走了。



查看完整回答
反对 回复 2019-11-06
?
炎炎设计

TA贡献1808条经验 获得超4个赞

您的文件应该直接位于项目文件夹下,而不是在任何其他子文件夹中。

如果您的项目的文件夹被命名为。AProject,它应该和你的在同一个地方src文件夹。

Aproject
        src
        word.txt


查看完整回答
反对 回复 2019-11-06
  • 3 回答
  • 0 关注
  • 1015 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信