有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何在Windows7上使用NIO库更改Java中的文件权限

我试图编写一段代码,以一个特定文件的路径对象开始,这样文件的所有者就不再有移动、删除或修改它的权限,但他们仍然可以读取它。还需要确保这可以撤消,并随时为管理员维护不受限制的访问权限

我的主要问题之一是,我不知道如何获取系统中的用户配置文件名和组名

一个深入的解释会很棒


共 (1) 个答案

  1. # 1 楼答案

    File类可以设置文件的读取、写入和执行。要通过用户获取它,需要使用NIO

    Files类和NIO使用PosixFilePermissions,它按文件和三个组(用户、所有者和组)设置权限。在Windows上,管理员和系统管理员将在一个组中

    要移动它,我们需要我们自己的安全管理器。移动文件时,NIO使用写入权限。因此,我们的安全管理器必须修改写入权限。以下面的代码为例

    另外,尽管文件系统提供程序是WindowsFileSystemProvider,但它是由FileSystemProviders返回的。getProvider(或类似的方法)。对于下载到的每个操作系统,rt.jar可能是不同的,但是如果你在Windows上,你可以假设这是正确的

    路径限制器。java

    package Testers;
    
    import java.io.IOException;
    import java.net.URI;
    import java.nio.channels.SeekableByteChannel;
    import java.nio.file.AccessMode;
    import java.nio.file.CopyOption;
    import java.nio.file.DirectoryStream;
    import java.nio.file.DirectoryStream.Filter;
    import java.nio.file.FileStore;
    import java.nio.file.FileSystem;
    import java.nio.file.Files;
    import java.nio.file.LinkOption;
    import java.nio.file.OpenOption;
    import java.nio.file.Path;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.nio.file.attribute.FileAttribute;
    import java.nio.file.attribute.FileAttributeView;
    import java.nio.file.attribute.PosixFilePermission;
    import java.nio.file.spi.FileSystemProvider;
    import java.util.HashSet;
    import java.util.Map;
    import java.util.Set;
    
    import sun.nio.fs.WindowsFileSystemProvider;
    
    
    public class PathRestrictor extends FileSystemProvider{
    
        boolean canRead;
        boolean canWrite;
        boolean canMove; //This is the tricky one
        boolean canOpen;
        private Path path;
        WindowsFileSystemProvider provider = new WindowsFileSystemProvider();
    
        public PathRestrictor(Path p){
            path = p;
            canRead = true;
            canWrite = true;
            canOpen = true;
        }
    
        public void setExecuteable(boolean executable){
            canOpen = executable;
            try {
                Files.setPosixFilePermissions(path, getPerms());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void setReadable(boolean readable){
            canRead = readable;
            try {
                Files.setPosixFilePermissions(path, getPerms());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void setWriteable(boolean writeable){
            canWrite = writeable;
            try {
                Files.setPosixFilePermissions(path, getPerms());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public void setMoveable(boolean moveable){
            canMove = moveable;
            MovementSecurityManager manager = new MovementSecurityManager();
            if(!moveable)manager.unMoveablePaths.add(path.toString());
            else manager.unMoveablePaths.remove(path.toString());
            System.setSecurityManager(manager);
    
    
        }
    
        private Set<PosixFilePermission> getPerms() {
            Set<PosixFilePermission> perms = new HashSet<PosixFilePermission>();
            perms.add(PosixFilePermission.GROUP_EXECUTE);
            perms.add(PosixFilePermission.GROUP_READ);
            perms.add(PosixFilePermission.GROUP_WRITE);
            if(canRead){
                perms.add(PosixFilePermission.OTHERS_READ);
                perms.add(PosixFilePermission.OWNER_READ);
            }
            if(canWrite){
                perms.add(PosixFilePermission.OTHERS_WRITE);
                perms.add(PosixFilePermission.OWNER_WRITE);
            }
            if(canOpen){
                perms.add(PosixFilePermission.OTHERS_EXECUTE);
                perms.add(PosixFilePermission.OWNER_EXECUTE);
            }
            return perms;
        }
    
        @Override
        public void checkAccess(Path path, AccessMode... modes) throws IOException {
            provider.checkAccess(path, modes);
        }
    
        @Override
        public void copy(Path source, Path target, CopyOption... options)
                throws IOException {
            // TODO Auto-generated method stub
            provider.copy(source, target, options);
    
        }
    
        @Override
        public void createDirectory(Path dir, FileAttribute<?>... attrs)
                throws IOException {
            provider.createDirectory(dir, attrs);
        }
    
        @Override
        public void delete(Path path) throws IOException {
            provider.delete(path);
        }
    
        @Override
        public <V extends FileAttributeView> V getFileAttributeView(Path path,
                java.lang.Class<V> type, LinkOption... options) {
            return provider.getFileAttributeView(path, type, options);
        }
    
        @Override
        public FileStore getFileStore(Path path) throws IOException {
            return provider.getFileStore(path);
        }
    
        @Override
        public FileSystem getFileSystem(URI uri) {
            return provider.getFileSystem(uri);
        }
    
        @Override
        public Path getPath(URI uri) {
            return provider.getPath(uri);
        }
    
        @Override
        public String getScheme() {
            return provider.getScheme();
        }
    
        @Override
        public boolean isHidden(Path path) throws IOException {
            return provider.isHidden(path);
        }
    
        @Override
        public boolean isSameFile(Path path, Path path2) throws IOException {
            return path.toString().equals(path2.toString());
        }
    
        @Override
        public void move(Path source, Path target, CopyOption... options)
                throws IOException {
                MovementSecurityManager manager = new MovementSecurityManager();
                manager.isMoving = true;
                System.setSecurityManager(manager);
                provider.move(source, target, options);
        }
    
        @Override
        public SeekableByteChannel newByteChannel(Path path,
                Set<? extends OpenOption> options, FileAttribute<?>... attrs)
                throws IOException {
            return provider.newByteChannel(path, options, attrs);
        }
    
        @Override
        public DirectoryStream<Path> newDirectoryStream(Path dir,
                Filter<? super Path> filter) throws IOException {
            return provider.newDirectoryStream(dir, filter);
        }
    
        @Override
        public FileSystem newFileSystem(URI uri, Map<String, ?> env)
                throws IOException {
            return provider.newFileSystem(uri, env);
        }
    
        @Override
        public <A extends BasicFileAttributes> A readAttributes(Path path,
                java.lang.Class<A> type, LinkOption... options) throws IOException {
            return provider.readAttributes(path, type, options);
        }
    
        @Override
        public Map<String, Object> readAttributes(Path path, String attributes,
                LinkOption... options) throws IOException {
            return provider.readAttributes(path, attributes, options);
        }
    
        @Override
        public void setAttribute(Path path, String attribute, Object value,
                LinkOption... options) throws IOException {
            provider.setAttribute(path, attribute, value, options);
    
        }
    
    }
    

    移动安全管理器。java

    package Testers;
    
    import java.util.HashSet;
    import java.util.Set;
    
    public class MovementSecurityManager extends SecurityManager {
    
        public Set<String> unMoveablePaths = new HashSet<String>();
        public boolean isMoving = true;
    
        public void checkWrite(String path){
            if(unMoveablePaths.contains(path) && isMoving) throw new SecurityException("Cannot move file!");
            else super.checkWrite(path);
        }
    
    }