java file.exists() 当文件存在时返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5434668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:07:21  来源:igfitidea点击:

file.exists() returning false when the file does exist

javaandroidfilefile-io

提问by Gady

In an Android application I'm working on, the user should be able to create a new CSV file on the SD card, named using text they input in an EditText.

在我正在开发的 Android 应用程序中,用户应该能够在 SD 卡上创建一个新的 CSV 文件,使用他们在 EditText 中输入的文本命名。

The problem is that after instantiating the File using the directory and filename, file.exists() returns false, even when the file does indeed exist at that location. I have browsed to SD card using an Android file browser and through Windows Explorer, and the file does exist.

问题是,在使用目录和文件名实例化 File 之后, file.exists() 返回false,即使该文件确实存在于该位置。我已经使用 Android 文件浏览器和 Windows 资源管理器浏览到 SD 卡,并且该文件确实存在。

Is this the correct way to check if the file already exists, and if so, what am I missing so that it returns true when it exists?

这是检查文件是否已经存在的正确方法,如果是,我缺少什么以便它在存在时返回true?

String csvname = edittext.getText().toString() + ".csv";
File sdCard = Environment.getExternalStorageDirectory(); //path returns "/mnt/sdcard"
File dir = new File (sdCard.getAbsolutePath() + "/Android/data/" + getPackageName() + "/files/"); // path returns "/mnt/sdcard/Android/data/com.phx.license/files"
dir.mkdirs();
File file = new File(dir, csvname); //path returns "/mnt/sdcard/Android/data/com.phx.license/files/Test.csv"

if(!file.exists()) //WHY DOES IT SAY IT DOESN'T EXIST WHEN IT DOES?
{
    ...
}

采纳答案by Bit

If you use createNewFile it will only create a file if it does not already exist.

如果您使用 createNewFile,它只会创建一个不存在的文件。

Java Files Documentation

Java 文件文档

public boolean createNewFile() throws IOException Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file. Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

public boolean createNewFile() throws IOException 当且仅当具有此名称的文件尚不存在时,以原子方式创建由此抽象路径名命名的新的空文件。检查文件是否存在以及如果文件不存在则创建文件是单个操作,对于可能影响该文件的所有其他文件系统活动而言是原子操作。注意:此方法不应用于文件锁定,因为无法使生成的协议可靠地工作。应该改用 FileLock 工具。

Returns: true if the named file does not exist and was successfully created; false if the named file already exists Throws: IOException - If an I/O error occurred SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the file Since: 1.2

返回: 如果指定的文件不存在且已成功创建,则返回 true;如果指定文件已存在则为 false 抛出:IOException - 如果发生 I/O 错误 SecurityException - 如果安全管理器存在且其 SecurityManager.checkWrite(java.lang.String) 方法拒绝对文件的写访问自:1.2

回答by jzd

Creating a new file object like so new File(dir, csvname);does not create a new file in the file system.

像这样创建新文件对象new File(dir, csvname);不会在文件系统中创建新文件。

You need to write data to it first.

您需要先向其中写入数据。

回答by Mahesh

I had the exact same issue but with yarn on hadoop where a spark job was trying to execute a command.

我遇到了完全相同的问题,但是在 hadoop 上使用了纱线,其中一个火花作业试图执行命令。

It was a file permission issue. I troubleshooted it by code like below which is in scala. exists and notExists both return false, which means the jvm is not able to tell if the file exists or not.

这是一个文件权限问题。我通过下面的代码解决了它,它在scala中。exists 和 notExists 都返回 false,这意味着 jvm 无法判断文件是否存在。

import java.nio.file.Path
import java.nio.file.Paths

val path = Paths.get(fileLocation);
println(":"+ Files.exists(path)+ ":" + Files.notExists(path))