【实践 -- 文件操作权限 -- java 】当前线程是否用户有 修改、删除该文件的权限!!! 要看操作系统了!!!

平时删除、修改文件,java需要通过securityManager查看当前线程是否有这些权限!!!

1、java.nio.file.Files

java.nio.file.Files


This class consists exclusively of static methods that operate on files, directories, or other types of files. 

In most cases, the methods defined here will **delegate to** the associated file system provider to perform the file operations.

2、java.io.File

public boolean delete() {
    SecurityManager security = System.getSecurityManager();
    if (security != null) {
        security.checkDelete(path);
    }
    if (isInvalid()) {
        return false;
    }
    return fs.delete(this);
}

security.checkDelete(path);

void java.lang.SecurityManager.checkDelete(String file)


Throws a SecurityException if the calling thread is not allowed to delete the specified file. 

This method is invoked for the current security manager by the delete method of class File. 

This method calls checkPermission with the FilePermission(file,"delete") permission. 

If you override this method, then you should make a call to super.checkDelete at the point the overridden method would normally throw an exception.
Parameters:file the system-dependent filename.Throws:SecurityException - if the calling thread does not have permission to delete the file.NullPointerException - if the file argument is null.See Also:java.io.File.delete()checkPermission
public void checkDelete(String file) {
    checkPermission(new FilePermission(file,
        SecurityConstants.FILE_DELETE_ACTION));
}

public void checkPermission(Permission perm) {
    java.security.AccessController.checkPermission(perm);
}
发布了381 篇原创文章 · 获赞 2 · 访问量 2256

猜你喜欢

转载自blog.csdn.net/m0_37681589/article/details/103800477
今日推荐