java - 文件存在时返回 false

标签 java filenotfoundexception

我有以下代码:

public static void main(String[] args){
    System.out.println(System.getProperty("user.dir"));
    File file = new File("/stuff.txt");
    System.out.println(file.exists());
}

但是,当我运行它时,尽管文件存在,但 file.exists() 返回 false。我检查了 System.getProperty("user.dir") 是否查看了正确的文件夹。我想我把文件放在正确的位置:结构如下:

-- filetest
    |-- FileTest.class
    |-- FileTest.java
    `-- stuff.txt

最佳答案

您忽略当前的user.dir并使用根文件夹/。要修复此问题,请删除 /。就像,

File file = new File("stuff.txt"); // <-- look for "stuff.txt" in the current folder

关于java - 文件存在时返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50748822/

相关文章:

java - FileInputStream - 如何直接获取路径,而不添加转义字符?

java - 从同一 JVM 运行 kafka 消费者和生产者时生产者速度缓慢

java - JDeveloper POJO Web 服务 FileNotFoundException

java.io.FileNotFoundException : (No such file or directory) - Working with servlets 错误

java - BitmapFactory 打开失败 : ENOENT (No such file or directory)

c# - Windows 10 更新后 System.DirectoryServices.AccountManagement.PrincipalContext 损坏

尽管文件存在,但 java.io.FileNotFoundException

java - 用 Java 逐行比较文本文件

Java Maven 清理并使用依赖项进行构建

java - java中的字符转换