栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java实现文件复制、剪切文件和删除示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java实现文件复制、剪切文件和删除示例

复制代码 代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class FileOperateDemo {

   
    public static boolean copyGeneralFile(String srcPath, String destDir) {
        boolean flag = false;
        File file = new File(srcPath);
        if(!file.exists()) { // 源文件或源文件夹不存在
            return false;
        }

        if(file.isFile()) {    // 文件复制
            flag = copyFile(srcPath, destDir);
        }
        else if(file.isDirectory()) { // 文件夹复制
            flag = copyDirectory(srcPath, destDir);
        }

        return flag;
    }

   
    public static boolean copyFile(String srcPath, String destDir) {
     return copyFile(srcPath, destDir, true); // 默认覆盖同名文件
    }

   
    public static boolean copyDirectory(String srcPath, String destDir) {
     return copyDirectory(srcPath, destDir, true);
    }

   
    public static boolean copyFile(String srcPath, String destDir, boolean overwriteExistFile) {
        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isFile()) { // 源文件不存在
            return false;
        }

        //获取待复制文件的文件名
        String fileName = srcFile.getName();
        String destPath = destDir + File.separator +fileName;
        File destFile = new File(destPath);
        if (destFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) { // 源文件路径和目标文件路径重复
            return false;
        }
        if(destFile.exists() && !overwriteExistFile) {    // 目标目录下已有同名文件且不允许覆盖
            return false;
        }

        File destFileDir = new File(destDir);
        if(!destFileDir.exists() && !destFileDir.mkdirs()) { // 目录不存在并且创建目录失败直接返回
         return false;
        }

        try {
            FileInputStream fis = new FileInputStream(srcPath);
            FileOutputStream fos = new FileOutputStream(destFile);
            byte[] buf = new byte[1024];
            int c;
            while ((c = fis.read(buf)) != -1) {
                fos.write(buf, 0, c);
            }
            fos.flush();
            fis.close();
            fos.close();

            flag = true;
        } catch (IOException e) {
            e.printStackTrace();
        }

        return flag;
    }

   
    public static boolean copyDirectory(String srcPath, String destDir, boolean overwriteExistDir) {
        if(destDir.contains(srcPath))
           return false;

        boolean flag = false;

        File srcFile = new File(srcPath);
        if (!srcFile.exists() || !srcFile.isDirectory()) { // 源文件夹不存在
            return false;
        }

        //获得待复制的文件夹的名字,比如待复制的文件夹为"E:\dir\"则获取的名字为"dir"
        String dirName = srcFile.getName();

        //目标文件夹的完整路径
        String destDirPath = destDir + File.separator + dirName + File.separator;
        File destDirFile = new File(destDirPath);
        if(destDirFile.getAbsolutePath().equals(srcFile.getAbsolutePath())) {
         return false;
        }
        if(destDirFile.exists() && destDirFile.isDirectory() && !overwriteExistDir) {    // 目标位置有一个同名文件夹且不允许覆盖同名文件夹,则直接返回false
            return false;
        }

        if(!destDirFile.exists() && !destDirFile.mkdirs()) {  // 如果目标目录不存在并且创建目录失败
         return false;
        }

        File[] fileList = srcFile.listFiles();    //获取源文件夹下的子文件和子文件夹
        if(fileList.length==0) {    // 如果源文件夹为空目录则直接设置flag为true,这一步非常隐蔽,debug了很久
            flag = true;
        }
        else {
            for(File temp: fileList) {
                if(temp.isFile()) {    // 文件
                    flag = copyFile(temp.getAbsolutePath(), destDirPath, overwriteExistDir);     // 递归复制时也继承覆盖属性
                }
                else if(temp.isDirectory()) {    // 文件夹
                    flag = copyDirectory(temp.getAbsolutePath(), destDirPath, overwriteExistDir);   // 递归复制时也继承覆盖属性
                }

                if(!flag) {
                    break;
                }
            }
        }

        return flag;
    }

   
    public static boolean deleteFile(String path) {
        boolean flag = false;

        File file = new File(path);
        if (!file.exists()) { // 文件不存在直接返回
            return flag;
        }

        flag = file.delete();

        return flag;
    }

   
   
    public static boolean cutGeneralFile(String srcPath, String destDir) {
     boolean flag = false;
        if(copyGeneralFile(srcPath, destDir) && deleteFile(srcPath)) { // 复制和删除都成功
         flag = true;
        }

        return flag;
    }

    public static void main(String[] args) {
     
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  // 一般正常场景
     System.out.println(copyGeneralFile("d://notexistfile", "d://test/"));      // 复制不存在的文件或文件夹
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/"));      // 待复制文件与目标文件在同一目录下
     System.out.println(copyGeneralFile("d://test/test.html", "d://test/test/"));  // 覆盖目标目录下的同名文件
     System.out.println(copyFile("d://test/", "d://test2", false)); // 不覆盖目标目录下的同名文件
     System.out.println(copyGeneralFile("d://test/test.html", "notexist://noexistdir/")); // 复制文件到一个不可能存在也不可能创建的目录下

     System.out.println("---------");

     
     System.out.println(copyGeneralFile("d://test/", "d://test2/"));

     System.out.println("---------");

     
     System.out.println(deleteFile("d://a/"));
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/152267.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号