简体   繁体   English

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

[英]file.exists() returning false when the file does exist

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. 问题是在使用目录和文件名实例化文件后,file.exists()返回false ,即使该文件确实存在于该位置。 I have browsed to SD card using an Android file browser and through Windows Explorer, and the file does exist. 我使用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?
{
    ...
}

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. public boolean createNewFile()throws IOException当且仅当具有此名称的文件尚不存在时,以原子方式创建一个由此抽象路径名命名的新空文件。 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. 应该使用FileLock工具。

Returns: true if the named file does not exist and was successfully created; 返回:如果指定的文件不存在且已成功创建,则返回true; 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 如果指定的文件已存在,则返回false抛出:IOException - 如果发生I / O错误SecurityException - 如果存在安全管理器且其SecurityManager.checkWrite(java.lang.String)方法拒绝对文件的写访问权限,则:1.2

Creating a new file object like so new File(dir, csvname); 创建一个新文件对象,如new File(dir, csvname); does not create a new file in the file system. 不会在文件系统中创建新文件。

You need to write data to it first. 您需要先将数据写入其中。

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. 我通过下面的代码在scala中对其进行了故障排除。 exists and notExists both return false, which means the jvm is not able to tell if the file exists or not. 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))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM