简体   繁体   English

exist()不起作用,但getAbsolutePath()起作用

[英]exists() does not work but getAbsolutePath() does work

I have the below code whereby I create a File type based on a pre-created file "test.brd" and also call the getAbsolutePath() method on this File, this all works correctly. 我有以下代码,可根据预先创建的文件“ test.brd”创建文件类型,并在此文件上调用getAbsolutePath()方法,所有这些都可以正常工作。 However, when I run the exists() method, this is deemed as not existing. 但是,当我运行exist()方法时,这被视为不存在。

When I debug, the status of the File is null and the path is also null, yet the getAbsolutePath() method works. 当我调试时,文件的状态为null,路径也为null,但是getAbsolutePath()方法仍然有效。 I have debugged and it goes to the Security section of the exists() method. 我已经调试,它进入了exist()方法的“安全性”部分。

Please see below: 请看下面:

File inputFile = new File("/Users/myname/Desktop/project_name/test.brd");
// The below works and returns the path
System.out.println(inputFile.getAbsolutePath());
if (inputFile.exists()) {
    System.out.println("Exists");
}
else {
    System.out.println("Invalid");
}

Even when I construct the file without the absolute path and just give the file name as a parameter (stored locally with Java file) the correct absolute path is provided. 即使当我在没有绝对路径的情况下构造文件,而只是将文件名作为参数(与Java文件本地存储)一起提供时,也会提供正确的绝对路径。

Hope this makes sense. 希望这是有道理的。 All I want to do is read a pre-created file into an Array, each character is an element in the array, I was intending on using scanner to read the file, but inputFile does not exist to be read. 我要做的就是将一个预先创建的文件读入一个Array中,每个字符都是该数组中的一个元素,我打算使用scanner来读取该文件,但是不存在要读取的inputFile

The two methods are about different aspects of the file: 这两种方法是关于文件的不同方面的:

  • getAbsolutePath() is about file name. getAbsolutePath()关于文件名。 In a way, this is a "string manipulation method" completely separated from the actual file system 在某种程度上,这是一种与实际文件系统完全分离的“字符串处理方法”
  • exists() is about the actual file. exists()与实际文件有关。 It checks whether or not the file is present in the file system at the location identified by the given path. 它检查在给定路径所标识的位置文件系统中是否存在文件。

Note that getAbsolutePath() and other path manipulation methods of File must work even without the file or the folder being present in the actual file system. 请注意,即使在实际文件系统中没有文件或文件夹的情况下, File getAbsolutePath()和其他路径操作方法也必须起作用。 Otherwise, the API would not be able to support file creation, eg through createNewFile() . 否则,API将无法支持文件创建,例如通过createNewFile()

If you take a look at the javadoc , you can find the following sentence 如果看一下javadoc ,可以找到以下句子

Instances of this class may or may not denote an actual file-system object such as a file or a directory. 此类的实例可以表示也可以不表示实际的文件系统对象,例如文件或目录。

Proving that the instance in memory of a File object is not necessarily a real file or directory existing in the file system. 证明File对象内存中的实例不一定是文件系统中存在的真实文件或目录。

File inputFile = new File("/Users/myname/Desktop/project_name/test.brd"); 文件inputFile = new File(“ / Users / myname / Desktop / project_name / test.brd”); The line above doesn't create a new File and hence it doesn't exists. 上面的行不会创建新文件,因此不存在。 If you want to create a file you can use method inputFile.createNewFile() . 如果要创建文件,可以使用方法inputFile.createNewFile() The method getAbsolutePath() works on the inputFile object and is completely different from file creation. 方法getAbsolutePath()对inputFile对象起作用,与文件创建完全不同。

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

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