文件操作工具类Files

public class FilesUtils {

    private static Logger logger = Logger.getLogger(FilesUtils.class);

    /**
     * 判断一个文件或文件夹是否存在,如果没有访问权限,返回false
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean exist(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        return Files.exists(fileDir,new LinkOption[] {LinkOption.NOFOLLOW_LINKS });
    }

    /**
     * 判断一个文件或文件夹是否存在,如果没有访问权限,返回false
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean notExist(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        return Files.notExists(fileDir,new LinkOption[] {LinkOption.NOFOLLOW_LINKS });
    }

    /**
     * 判断是否有读的权限
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean isReadable(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        return Files.isReadable(fileDir);
    }

    /**
     * 判断是否有写的权限
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean isWritable(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        return Files.isWritable(fileDir);
    }

    /**
     * 判断是否有执行的权限
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean isExecutable(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        return Files.isExecutable(fileDir);
    }

    /**
     * 判断是否是一个文件
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean isRegularFile(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        return Files.isRegularFile(fileDir);
    }

    /**
     * 判断两个文件是否是同一个文件
     * @time:2017年7月10日
     * @author:MAZHEN
     * @throws IOException 
     * @description
     */
    public static boolean isRegularFile(String path1,String path2) throws IOException{
        Path file1 = FileSystems.getDefault().getPath(path1);
        Path file2 = FileSystems.getDefault().getPath(path2);
        return Files.isSameFile(file1,file2);
    }

    /**
     * 判断是否为隐藏文件
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean isHidden(String path) throws IOException{
        Path fileDir = FileSystems.getDefault().getPath(path);
        return Files.isHidden(fileDir);
    }

    /**
     * 获取系统根目录
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static Iterable<Path> isHidden(){
        return FileSystems.getDefault().getRootDirectories();  
    }

    /**
     * 根据给定的文件夹名创建目录
     * @time:2017年7月9日
     * @author:MAZHEN
     * @description
     */
    public static boolean createDirectory(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        try {
            Files.createDirectory(fileDir);
            return true;
        } catch (IOException e) {
            logger.error("创建文件夹失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 根据指定路径创建文件夹,并设置权限
     * @time:2017年7月9日
     * @author:MAZHEN
     * @description
     */
    public static boolean createDirectory(String path,String permission){
        Path fileDir = FileSystems.getDefault().getPath(path);
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString(permission); 
        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
        try {
            Files.createDirectory(fileDir, attr);
            return true;
        } catch (IOException e) {
            logger.error("创建文件夹失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 根据指定路径创建多层文件夹
     * @time:2017年7月9日
     * @author:MAZHEN
     * @description
     */
    public static boolean createDirectories(String path){
        Path fileDir = FileSystems.getDefault().getPath(path);
        try {
            Files.createDirectories(fileDir);
            return true;
        } catch (IOException e) {
            logger.error("创建文件夹失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 根据指定路径创建多层文件夹,并设置权限
     * @time:2017年7月9日
     * @author:MAZHEN
     * @description
     */
    public static boolean createDirectories(String path,String permission){
        Path fileDir = FileSystems.getDefault().getPath(path);
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString(permission); 
        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
        try {
            Files.createDirectories(fileDir, attr);
            return true;
        } catch (IOException e) {
            logger.error("创建文件夹失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 获取指定文件夹下所有的文件
     * @time:2017年7月9日
     * @author:MAZHEN
     * @description
     */
    public static DirectoryStream<Path> listFile(String path) throws IOException{
        Path dir = FileSystems.getDefault().getPath(path);  
        DirectoryStream<Path> ds = Files.newDirectoryStream(dir);
        return ds;
    }

    /**
     * 获取指定文件夹满足正则的所有文件
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static DirectoryStream<Path> listFile(String path,String regular) throws IOException{
        Path dir = FileSystems.getDefault().getPath(path);  
        DirectoryStream<Path> ds = Files.newDirectoryStream(dir,regular);
        return ds;
    }

    /**
     * 创建指定文件
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean createFile(String path){
        Path file = FileSystems.getDefault().getPath(path);
        try {
            Files.createFile(file);
            return true;
        } catch (IOException e) {
            logger.error("创建文件失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 创建指定文件,并设置权限
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean createFile(String path,String permission){
        Path file = FileSystems.getDefault().getPath(path);
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString(permission); 
        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
        try {
            Files.createFile(file, attr);
            return true;
        } catch (IOException e) {
            logger.error("创建文件夹失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 将数据写入指定文件,默认编码集为UTF-8
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean write(String path,String content){
        return write(path,content,"UTF-8");
    }

    /**
     * 将数据写入指定的文件,指定编码集
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean write(String path,String content,String charset){
        Path file = FileSystems.getDefault().getPath(path);
        try {
            byte[] rf_wiki_byte = content.getBytes(charset); 
            Files.write(file, rf_wiki_byte);
            return true;
        } catch (Exception e) {
            logger.error("写入文件失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 将字节数组写入指定文件
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static boolean write(String path,byte[] content){
        Path file = FileSystems.getDefault().getPath(path);
        try {
            Files.write(file, content);
            return true;
        } catch (Exception e) {
            logger.error("写入文件失败,错误信息:"+e);
            return false;
        }
    }

    /**
     * 读取指定文件中的数据为字节流
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static byte[] readAllBytes(String path) throws IOException{
        Path file = FileSystems.getDefault().getPath(path);
        return Files.readAllBytes(file);
    }

    /**
     * 读取指定文本文件中的所有行,返回为行的list
     * @time:2017年7月10日
     * @author:MAZHEN
     * @throws IOException 
     * @description
     */
    public static List<String> readAllLines(String path) throws IOException{
        return readAllLines(path,"UTF-8");
    }

    /**
     * 读取指定文本文件中的所有行,返回为行的list
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static List<String> readAllLines(String path,String charsetStr) throws IOException{
        Charset charset = Charset.forName(charsetStr);
        Path file = FileSystems.getDefault().getPath(path);
        return Files.readAllLines(file,charset);
    }

    /**
     * 以默认编码集读取指定文本文件中的数据
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static String read(String path) throws IOException{
        return read(path,"UTF-8");
    }

    /**
     * 根据指定的编码集读取文本文件中的数据
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static String read(String path,String charsetStr) throws IOException{
        Charset charset = Charset.forName(charsetStr);
        Path file = FileSystems.getDefault().getPath(path);
        List<String> lines = Files.readAllLines(file, charset);
        if(lines != null && !lines.isEmpty()){
            StringBuffer sb = new StringBuffer();
            for (String line : lines) {
                sb.append(line);
            }
            return sb.toString();
        }
        return null;
    }

    /**
     * 获取默认编码集(UTF-8)的BufferedWriter
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static BufferedWriter newBufferedWriter(String path) throws IOException{
        return newBufferedWriter(path,"UTF-8");
    }

    /**
     * 获取指定编码集的BufferedWriter
     * @time:2017年7月10日
     * @author:MAZHEN
     * @description
     */
    public static BufferedWriter newBufferedWriter(String path,String charsetStr) throws IOException{
        Charset charset = Charset.forName(charsetStr);
        Path file = FileSystems.getDefault().getPath(path);
        return Files.newBufferedWriter(file, charset, StandardOpenOption.APPEND);
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值