`
yuancihang
  • 浏览: 142608 次
  • 性别: Icon_minigender_1
  • 来自: 洛阳
社区版块
存档分类
最新评论

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

F# 
阅读更多

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();
    }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics