复制删除移动文件和文件夹

1. 移动文件或者文件夹

/**
     * 移动文件或者文件夹,如从e:/aa.txt到e:/tmp/aa.txt, 从e:/aa到e:/bb/aa
     * @param input String
     * @param output String
     */
    public static void move(String input, String output){
        File inputFile = new File(input);
        File outputFile = new File(output);
        try {
          inputFile.renameTo(outputFile);
        } catch (Exception ex) {
          ex.printStackTrace();
        }
     }//move

 

2. 复制单个文件

 

/**
     * 复制单个文件
     * @param source
     * @param dest
     * @throws IOException
     */
    public static void copyFile(String source, String dest)throws IOException{
        final int BUFSIZE = 65536;
        File f = new File(source);
        File f2 = new File(dest);
        if(!f.exists()){
            return ;
        }
        if(!f2.exists()){
            File parent = new File(f2.getParent()); //得到父文件夹
            if(!parent.exists()){
                parent.mkdirs();
            }
            f2.createNewFile();
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(f));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
        byte[] buf = new byte[BUFSIZE];
        while((bis.read(buf))!=-1){
            bos.write(buf);
        }
        bos.close();
        bis.close();
    }

3. 复制文件夹及其子文件夹

/**
     * 复制文件夹及其子文件夹
     * @param source String 源文件夹,如: d:/tmp
     * @param dest String 目标文件夹,如: e:/tmp
     * @throws IOException
     */
    public static void copyFolder(String source, String dest)throws IOException{
       
        File f1 = new File(source);
        File f2 = new File(dest);
        if(!f1.exists()){
            return ;
        }
        if((!f2.exists())&&(f1.isDirectory())){
            f2.mkdirs();
        }
        String[] fileList = f1.list();
        if(fileList == null){
            return ;
        }
        for(String file:fileList){
            String newSource = f1.getAbsolutePath() + File.separator + file;
            String newDest = f2.getAbsolutePath() + File.separator + file;
            if(new File(newSource).isDirectory()){
                logger.info("正在复制文件夹从"+newSource+"到"+newDest+"。。。");
                copyFolder(newSource, newDest);
            }else{
                logger.info("正在复制文件从"+newSource+"到"+newDest+"。。。");
                copyFile(newSource, newDest);
            }
        }//for
    }

 

4. 删除某个文件夹

/**
     *  删除某个文件夹下的所有文件和所有子文件夹, 不包括它自己
     * @param folder String
     * @param delFolder boolean 是否连文件夹一起删除
     * @throws IOException
     */
    public static void deleteAllFiles(String folder, boolean delFolder)throws IOException {
        File f1 = new File(folder);

        if (!f1.exists()) {
            return;
        }
        String[] fileList = f1.list();
        if (fileList == null) { //空文件夹
            if(delFolder){
                logger.info("正在删除文件夹"+f1.getAbsolutePath()+"。。。");
                f1.delete(); //删除文件夹
            }
            return;
        }
        for (String file : fileList) {
            String newSource = f1.getAbsolutePath() + File.separator + file;
            if (new File(newSource).isDirectory()) {
                deleteAllFiles(newSource, delFolder);
                if(delFolder){
                    logger.info("正在删除文件夹"+newSource+"。。。");
                    new File(newSource).delete(); //删除文件夹
                }
            } else {
                logger.info("正在删除文件"+newSource+"。。。");
                new File(newSource).delete();
            }
        }//for
    }

/**
     * 删除整个文件夹,包括它本身
     * @param dir String
     * @throws IOException
     */
    public static void deleteFolder(String dir)throws IOException{
        deleteAllFiles(dir, true);
        File f = new File(dir);
        logger.info("正在删除文件夹"+f.getAbsolutePath()+"...");
        f.delete();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值