JDK 1.7当中的文件属性、权限操作

在JDK1.7当中新增了对文件操作的一些API接口,此Demo特点:

  • 读取文件的属性
  • 读取文件的权限,RWX格式,Number格式
  • 修改文件的权限
  • 权限格式的转换:RWX格式与Number格式

二次修改说明:

  • 参数命名 首字母小写
  • 添加异常捕获,需要引用第三方apache BizCoreException jar包

package com.idcos.cloud.core.common.util;

import java.io.File;
import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.attribute.GroupPrincipal;
import java.nio.file.attribute.PosixFileAttributeView;
import java.nio.file.attribute.PosixFileAttributes;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.nio.file.attribute.UserPrincipal;
import java.nio.file.attribute.UserPrincipalLookupService;
import java.util.Set;

import com.idcos.common.biz.BizCoreException;

public class FileAttributesUtil {

    /**
     * 获取文件系统信息
     * @param file
     * @return
     */
    public static FileSystem getFileSystem(File file) {
        return file.toPath().getFileSystem();
    }

    public static UserPrincipalLookupService getUserPrincipalLookupService(File file) {
        return getFileSystem(file).getUserPrincipalLookupService();
    }

    public static GroupPrincipal getGroupPrincipal(File file, String groupName) {
        try {
            return getUserPrincipalLookupService(file).lookupPrincipalByGroupName(groupName);
        } catch (IOException e) {
            throw new BizCoreException("获取group名称:" + groupName + "异常", e);
        }
    }

    public static UserPrincipal getUserPrincipal(File file, String userName) {
        try {
            return getUserPrincipalLookupService(file).lookupPrincipalByName(userName);
        } catch (IOException e) {
            throw new BizCoreException("获取user名称:" + userName + "异常", e);
        }
    }

    public static PosixFileAttributeView getPosixFileAttributeView(File file) {
        return Files.getFileAttributeView(file.toPath(), PosixFileAttributeView.class);
    }

    public static PosixFileAttributes getPosixFileAttributes(File file) {
        try {
            return getPosixFileAttributeView(file).readAttributes();
        } catch (IOException e) {
            throw new BizCoreException("获取文件属性异常", e);
        }
    }

    /**
     * 修改文件用户组
     * @param file 文件
     * @param groupName 用户组名称
     * @throws IOException
     */
    public static void setFileGroup(File file, String groupName) {
        try {
            getPosixFileAttributeView(file).setGroup(getGroupPrincipal(file, groupName));
        } catch (IOException e) {
            throw new BizCoreException("写入文件group信息异常", e);
        }

    }

    /**
     * 修改文件用户
     * @param file 文件
     * @param userName 用户名称
     * @throws IOException
     */
    public static void setFileUser(File file, String userName) {
        try {
            getPosixFileAttributeView(file).setOwner(getUserPrincipal(file, userName));
        } catch (IOException e) {
            throw new BizCoreException("写入文件user信息异常", e);
        }
    }

    /**
     * 修改文件权限
     * @param file 文件 
     * @param posixFilePermissionNumber 数字类型的权限,如:777
     * @throws IOException
     */
    public static void setFilePermissions(File file, String posixFilePermissionNumber) {
        try {
            getPosixFileAttributeView(file)
                .setPermissions(
                    parsePosixFilePermissiontoSet(parsePosixFilePermissiontoRWX(posixFilePermissionNumber)));
        } catch (IOException e) {
            throw new BizCoreException("写入文件权限信息异常", e);
        }
    }

    /**
     * 修改文件权限
     * @param file 文件
     * @param perms 文件权限数组
     * @throws IOException
     */
    public static void setFilePermissions(File file, Set<PosixFilePermission> perms) {
        try {
            getPosixFileAttributeView(file).setPermissions(perms);
        } catch (IOException e) {
            throw new BizCoreException("写入文件权限信息异常", e);
        }
    }

    /**
     * 将RWX类型的文件权限修改为Set
     * @param posixFilePermission 
     * @return
     */
    public static Set<PosixFilePermission> parsePosixFilePermissiontoSet(String posixFilePermission) {
        return PosixFilePermissions.fromString(posixFilePermission);
    }

    /**
     * 将String rwx类型的权限转换为String 数字类型
     * 如:rw-r----- 转换为 640
     * @param PosixFilePermission
     * @return
     */
    public static String parsePosixFilePermissiontoNum(Set<PosixFilePermission> set) {
        int user = 0;
        int group = 0;
        int other = 0;
        for (PosixFilePermission permission : set) {

            switch (permission) {

                case OWNER_READ:
                    user = user | 4;
                    break;
                case OWNER_WRITE:
                    user = user | 2;
                    break;
                case OWNER_EXECUTE:
                    user = user | 1;
                    break;
                case GROUP_READ:
                    group = group | 4;
                    break;
                case GROUP_WRITE:
                    group = group | 2;
                    break;
                case GROUP_EXECUTE:
                    group = group | 1;
                    break;
                case OTHERS_READ:
                    other = other | 4;
                    break;
                case OTHERS_WRITE:
                    other = other | 2;
                    break;
                case OTHERS_EXECUTE:
                    other = other | 1;
                    break;
            }
        }
        return "" + user + group + other;
    }

    /**
     * 
     * 将String 数字类型的权限转换为String rwx类型
     * 如:640 转换为 rw-r----- 
     * @param posixFilePermission
     * @return
     */
    public static String parsePosixFilePermissiontoRWX(String posixFilePermission) {
        String returnString = "";
        char[] perStringArr = posixFilePermission.toCharArray();
        for (char x : perStringArr) {
            if ((x & 4) == 4) {
                returnString += "r";
            } else {
                returnString += "-";
            }
            if ((x & 2) == 2) {
                returnString += "w";
            } else {
                returnString += "-";
            }
            if ((x & 1) == 1) {
                returnString += "x";
            } else {
                returnString += "-";
            }
        }

        return returnString;
    }
}

posted @ 2015-08-05 12:02  peng_gy  阅读(214)  评论(0编辑  收藏  举报