FileUtils

摘要:
BooleanisNew){Filefile=newFile(filePath);file.exists()&//当前时间Stringrandom=RandomUtils.generateNumberString(10);
package com.JUtils.file;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigInteger;
import java.security.MessageDigest;

import com.JUtils.date.DateUtils;
import com.JUtils.math.RandomUtils;

/**
 * @desc:文件工具类
 * 
 * 
 * 
 */
public class FileUtils {
    private static final String FOLDER_SEPARATOR = "/";
    private static final char EXTENSION_SEPARATOR = '.';
    
    /**
     * @desc:判断指定路径是否存在,如果不存在,根据参数决定是否新建
     * @autor:chenssy
     * @date:2014年8月7日
     *
     * @param filePath
     *             指定的文件路径
     * @param isNew
     *             true:新建、false:不新建
     * @return 存在返回TRUE,不存在返回FALSE
     */
    public static boolean isExist(String filePath,boolean isNew){
        File file = new File(filePath);
        if(!file.exists() && isNew){    
            return file.mkdirs();    //新建文件路径
        }
        return false;
    }
    
    /**
     * 获取文件名,构建结构为 prefix + yyyyMMddHH24mmss + 10位随机数 + suffix + .type
     * @autor:chenssy
     * @date:2014年8月11日
     *
     * @param type
     *                 文件类型
     * @param prefix
     *                 前缀
     * @param suffix
     *                 后缀
     * @return
     */
    public static String getFileName(String type,String prefix,String suffix){
        String date = DateUtils.getCurrentTime("yyyyMMddHH24mmss");   //当前时间
        String random = RandomUtils.generateNumberString(10);   //10位随机数
        
        //返回文件名  
        return prefix + date + random + suffix + "." + type;
    }
    
    /**
     * 获取文件名,文件名构成:当前时间 + 10位随机数 + .type
     * @autor:chenssy
     * @date:2014年8月11日
     *
     * @param type
     *                 文件类型
     * @return
     */
    public static String getFileName(String type){
        return getFileName(type, "", "");
    }
    
    /**
     * 获取文件名,文件构成:当前时间 + 10位随机数
     * @autor:chenssy
     * @date:2014年8月11日
     *
     * @return
     */
    public static String getFileName(){
        String date = DateUtils.getCurrentTime("yyyyMMddHH24mmss");   //当前时间
        String random = RandomUtils.generateNumberString(10);   //10位随机数
        
        //返回文件名  
        return date + random;
    }
    
    /**
     * 获取指定文件的大小
     *
     * @param file
     * @return
     * @throws Exception
     *
     * @author:chenssy
     * @date : 2016年4月30日 下午9:10:12
     */
    @SuppressWarnings("resource")
    public static long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            file.createNewFile();
        }
        return size;
    }
    
    /**
     * 删除所有文件,包括文件夹
     * 
     * @author : chenssy
     * @date : 2016年5月23日 下午12:41:08
     *
     * @param dirpath
     */
    public void deleteAll(String dirpath) {  
         File path = new File(dirpath);  
         try {  
             if (!path.exists())  
                 return;// 目录不存在退出   
             if (path.isFile()) // 如果是文件删除   
             {  
                 path.delete();  
                 return;  
             }  
             File[] files = path.listFiles();// 如果目录中有文件递归删除文件   
             for (int i = 0; i < files.length; i++) {  
                 deleteAll(files[i].getAbsolutePath());  
             }  
             path.delete();  

         } catch (Exception e) {  
             e.printStackTrace();  
         }   
    }
    
    /**
     * 复制文件或者文件夹
     * 
     * @author : chenssy
     * @date : 2016年5月23日 下午12:41:59
     *
     * @param inputFile
     *                         源文件
     * @param outputFile
     *                         目的文件
     * @param isOverWrite
     *                         是否覆盖文件
     * @throws java.io.IOException
     */
    public static void copy(File inputFile, File outputFile, boolean isOverWrite)
            throws IOException {
        if (!inputFile.exists()) {
            throw new RuntimeException(inputFile.getPath() + "源目录不存在!");
        }
        copyPri(inputFile, outputFile, isOverWrite);
    }
    
    /**
     * 复制文件或者文件夹
     * 
     * @author : chenssy
     * @date : 2016年5月23日 下午12:43:24
     *
     * @param inputFile
     *                         源文件
     * @param outputFile
     *                         目的文件
     * @param isOverWrite
     *                         是否覆盖文件
     * @throws java.io.IOException
     */
    private static void copyPri(File inputFile, File outputFile, boolean isOverWrite) throws IOException {
        if (inputFile.isFile()) {        //文件
            copySimpleFile(inputFile, outputFile, isOverWrite);
        } else {
            if (!outputFile.exists()) {        //文件夹    
                outputFile.mkdirs();
            }
            // 循环子文件夹
            for (File child : inputFile.listFiles()) {
                copy(child, new File(outputFile.getPath() + "/" + child.getName()), isOverWrite);
            }
        }
    }
    
    /**
     * 复制单个文件
     * 
     * @author : chenssy
     * @date : 2016年5月23日 下午12:44:07
     *
     * @param inputFile
     *                         源文件
     * @param outputFile
     *                         目的文件
     * @param isOverWrite
     *                         是否覆盖
     * @throws java.io.IOException
     */
    private static void copySimpleFile(File inputFile, File outputFile,
            boolean isOverWrite) throws IOException {
        if (outputFile.exists()) {
            if (isOverWrite) {        //可以覆盖
                if (!outputFile.delete()) {
                    throw new RuntimeException(outputFile.getPath() + "无法覆盖!");
                }
            } else {
                // 不允许覆盖
                return;
            }
        }
        InputStream in = new FileInputStream(inputFile);
        OutputStream out = new FileOutputStream(outputFile);
        byte[] buffer = new byte[1024];
        int read = 0;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
        in.close();
        out.close();
    }
    
    /**
     * 获取文件的MD5
     * 
     * @author : chenssy
     * @date : 2016年5月23日 下午12:50:38
     *
     * @param file
     *                 文件
     * @return
     */
    public static String getFileMD5(File file){
        if (!file.exists() || !file.isFile()) {  
            return null;  
        }  
        MessageDigest digest = null;  
        FileInputStream in = null;  
        byte buffer[] = new byte[1024];  
        int len;  
        try {  
            digest = MessageDigest.getInstance("MD5");  
            in = new FileInputStream(file);  
            while ((len = in.read(buffer, 0, 1024)) != -1) {  
                digest.update(buffer, 0, len);  
            }  
            in.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
            return null;  
        }  
        BigInteger bigInt = new BigInteger(1, digest.digest());  
        return bigInt.toString(16);  
    }
    
    /**
     * 获取文件的后缀
     * 
     * @author : chenssy
     * @date : 2016年5月23日 下午12:51:59
     *
     * @param file
     *                 文件
     * @return
     */
    public static String getFileSuffix(String file) {
        if (file == null) {
            return null;
        }
        int extIndex = file.lastIndexOf(EXTENSION_SEPARATOR);
        if (extIndex == -1) {
            return null;
        }
        int folderIndex = file.lastIndexOf(FOLDER_SEPARATOR);
        if (folderIndex > extIndex) {
            return null;
        }
        return file.substring(extIndex + 1);
    }
    
    /**
     * 文件重命名
     * 
     * @author : chenssy
     * @date : 2016年5月23日 下午12:56:05
     *
     * @param oldPath
     *                     老文件
     * @param newPath
     *                     新文件
     */
    public boolean renameDir(String oldPath, String newPath) {  
        File oldFile = new File(oldPath);// 文件或目录   
        File newFile = new File(newPath);// 文件或目录   
        
        return oldFile.renameTo(newFile);// 重命名   
    }
}

免责声明:文章转载自《FileUtils》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇WPF界面开发:DevExpress WPF在GridControl中固定行时处理时刻接口测试中抓包工具的使用下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

redhat7 升级openssh openssl

部署telnet,防止ssh启动失败  1、关闭防火墙或者开放23端口  2、安装启动服务,并开启root访问 yum install -y telnet-server.x86_64 yum install -y telnet.x86_64 yum install -y xinetd.x86_64 systemctl enable xinetd.ser...

【CCS仿真】如何将CCS仿真时memory中的数据以Hex、Integer、 Long 、Float、 Addressable Unit类型保存到PC

2013-12-04 19:07:05 将在CCS中仿真的数据导入电脑上时,可以选择不同的数据类型,以便分析,具体方法如下: 在CCS菜单中,选择File—>Data—>Save,弹出以下窗口:                         在文件名中输入要保存的文件的名字,在保存类型中可以选择保存的文件类型以及格式。文件类型有dat文件与...

Gi命令行操作

一、本地库初始化   命令:git init   效果:          注意:.git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱修改 二、设置签名   形式     用户名:user     Email 地址:user@123.com   作用:区分不同开发人员的身份   辨析:这里设置的签名和登录远程库(代码托管中心)的账号、密码...

搭建Modelsim SE仿真环境-使用do文件仿真

摘要: 本章我们介绍仿真环境搭建是基于ModelsimSE的。Modelsim有很多版本,比如说Modelsim-Altera,但是笔者还是建议大家使用Modelsim-SE,Modelsim-Altera实际是针对Altera 的OEM版本,它事先将Altera的一些IP核仿真库添加到了工具中,但功能上有一些缩减。而Modelsim-SE需要自己手动添加...

Tornado web 框架

Tornado web 框架 其实很简单、深度应用一、简介     Tornado 是 FriendFeed 使用的可扩展的非阻塞式 web 服务器及其相关工具的开源版本。这个 Web 框架看起来有些像web.py 或者 Google 的 webapp,不过为了能有效利用非阻塞式服务器环境,这个 Web 框架还包含了一些相关有用工具及优化。     Tor...

使用notepad++调用vlog.exe程序编译verilog代码

经常使用notepad++的编辑器编写verilog代码,经常调用modelsim的进行基本编码输入检查 。但是每次都手动打开modelsim软件既费时间又由于启动modelsim GUI占用系统比较大的内存, 于是博主就经过研究notepad++工具,找到了一个直接在notepad++ 客户端运行vlog.exe来对verilog代码进行编译的办法 。打...