File.exists()如何在JAVA中工作?

时间:2013-07-29 14:49:58

标签: java file-io

File.exists()通过比较路径,名称或文件内容来判断文件是否与另一个文件相同吗?

3 个答案:

答案 0 :(得分:2)

SourceCode

   public boolean exists() {
             SecurityManager security = System.getSecurityManager();
             if (security != null) {
                 security.checkRead(path);
             }
             return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
         }

如果仔细观察,它所做的就是通过调用文件系统fs来检查权限,当然还有existing位:)

答案 1 :(得分:0)

From source code

  

测试此抽象路径名表示的文件或目录是否存在。

     

返回:

     

当且仅当此抽象路径名表示的文件或目录存在时才为true;否则是假的

   public boolean More ...exists() {
746         SecurityManager security = System.getSecurityManager();
747         if (security != null) {
748             security.checkRead(path);
749         }
750         return ((fs.getBooleanAttributes(this) & FileSystem.BA_EXISTS) != 0);
751     }

答案 2 :(得分:0)

File.exists()决定文件是否存在。
根据{{​​3}}

public boolean exists()
Tests whether the file or directory denoted by this abstract pathname exists.

如果你想通过路径知道两个文件是否相同。你应该使用
equals()类的File方法。根据Docs

public boolean equals(Object obj)

Tests this abstract pathname for equality with the given object.  
Returns true if and only if the argument is not null and is an abstract 
pathname that denotes the same file or directory as this abstract pathname.
相关问题