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

【java】File类与目录拷贝

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

【java】File类与目录拷贝

File类基本方法:

public class FileTest01 {
    public static void main(String[] args) throws Exception{
        //创建File对象
        File fi = new File("file");
        //判断是否存在
        System.out.println(fi.exists());

        //若file不存在,则以文件的形式创建出来
        if(!fi.exists()){
            fi.createNewFile();
        }

        //若file不存在,则以目录(文件夹)的形式创建出来
         if(!fi.exists()){
            fi.mkdir();
         }

        //创建多重目录
        File f1 = new File("D:/a/b/c/d");
        if(!f1.exists()){
            f1.mkdirs();
        }

        File f2 = new File("D:\java\a\b");
        //获取文件的父路径(上一级路径)
        String parentPath = f2.getParent();
        //获取绝对路径
        System.out.println(f2.getAbsolutePath());
    }
}
public class FileTest02 {
    public static void main(String[] args) {
        File f1 = new File("/Users/mac/IdeaProjects/untitled/data");
        //获取文件名
        System.out.println(f1.getName());
        //判断是否是一个目录
        System.out.println(f1.isDirectory());
        //判断是否是一个文件
        System.out.println(f1.isFile());
        //获取文件或目录最后一次修改时间
        long ms = f1.lastModified(); //这个毫秒是从1970到现在的总毫秒
        //将总毫秒转换成日期
        Date time = new Date(ms);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss SSS");
        String strTime = sdf.format(time);
        System.out.println(strTime);
        //获取文件大小
        System.out.println(f1.length());//单位为字节
    }
}
public class FileTest03 {
    public static void main(String[] args) {
        //File[] listFiles()
        File file = new File("/Users/mac/IdeaProjects/untitled");
        File[] files = file.listFiles();
        //foreach
        for(File f : files){
            System.out.println(f.getName());
        }
    }
}

目录拷贝:

public class CopyAll {
    public static void main(String[] args) {
        //拷贝源
        File srcFile = new File("E:\c");
        //拷贝目标
        File destFile = new File("E:\新建文件夹");
        //统计拷贝耗时
        long begin = System.currentTimeMillis();
        copyDir(srcFile,destFile);
        long end = System.currentTimeMillis();
        System.out.println("拷贝耗时:"+ (end - begin) + "ms");

    }

    
    public static void copyDir(File srcFile,File destFile){
        if(srcFile.isFile()){
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                in = new FileInputStream(srcFile);
                String paths = (destFile.getAbsolutePath().endsWith("\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\") + srcFile.getAbsolutePath().substring(3);
                out = new FileOutputStream("paths");

                //一边读一边写
                byte[] bytes = new byte[1024 * 1024];
                int readCount = 0;
                while((readCount = in.read())!= -1){
                    out.write(bytes,0,readCount);
                }

                out.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (in != null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return;
        }
        //获取源下面的子目录
        File[] files = srcFile.listFiles();
        for(File f : files){
            if(f.isDirectory()){
                //新建对应目录
                String srcDir = f.getAbsolutePath();
                String destDir = (destFile.getAbsolutePath().endsWith("\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\") + srcDir.substring(3);
                File newFile = new File(destDir);
                if(!newFile.exists()){
                    newFile.mkdirs();
                }
            }
            //递归调用
            copyDir(f,destFile);

        }
    }
}

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

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

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