java - 系统找不到java中指定的文件

标签 java file

我正在制作一个打开和读取文件的程序。 这是我的代码:

import java.io.*;

public class FileRead{
    public static void main(String[] args){
        try{
            File file = new File("hello.txt");
            System.out.println(file.getCanonicalPath());
            FileInputStream ft = new FileInputStream(file);

            DataInputStream in = new DataInputStream(ft);
            BufferedReader br = new BufferedReader(new InputStreamReader(in));
            String strline;

            while((strline = br.readLine()) != null){
                System.out.println(strline);
            }
            in.close();
        }catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }
    }
}

但是当我运行时,我得到了这个错误:

C:\Users\User\Documents\Workspace\FileRead\hello.txt
Error: hello.txt (The system cannot find the file specified)

我的 FileRead.javahello.txt 在同一目录中可以找到:

C:\Users\User\Documents\Workspace\FileRead

我想知道我做错了什么?

最佳答案

尝试通过调用列出目录中所有文件的名称:

File file = new File(".");
for(String fileNames : file.list()) System.out.println(fileNames);

看看你是否会在列表中找到你的文件。

关于java - 系统找不到java中指定的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11553042/

相关文章:

r - 在 R 中打开 100 个文件

arrays - 使用CodeIgniter 2.0上传多个文件(数组)

java - 将大量数据从内存写入文件的最快方法是什么?

java - 从标签中删除命名空间

java - 多线程程序中出现意外结果

java - 为什么不使用工具来扩展工作?

java - 该类型的方法未定义。帮我解决它

file - 使用命令行工具将文件/文件夹添加到 Visual Studio 项目

java - 从 Java 小程序访问输入类型 ="file"完整路径

java - 将 App Engine 数据存储区低级 API 与 Java 结合使用