Java基础进阶IO流-File类

导读:本篇文章讲解 Java基础进阶IO流-File类,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

File类

示例代码01:

public class FileTest01 {
    public static void main(String[] args) {
        File file = new File("file");
        //System.out.println(file.exists());

        //以文件形式创建
        /*if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/

        //以目录形式创建
        if(!file.exists()) {
            file.mkdir();
        }

        //以多级目录形式创建
        if(!file.exists()){
            file.mkdirs();
        }

        //获取文件父路径
        String parent = file.getParent();
        System.out.println(parent);
        //获取文件绝对路径
        File absoluteFile = file.getAbsoluteFile();
        System.out.println("文件的绝对路径为:" + absoluteFile);
        //获取文件绝对路径
        String absolutePath = file.getAbsolutePath();
        System.out.println("文件的绝对路径为:" + absolutePath);
    }
}

运行结果:

在这里插入图片描述

File类的常用方法

示例代码02:

public class FileTest02 {
    public static void main(String[] args) {
        File file = new File("D:\\必须软件\\毕业设计\\基于JSP的公司文档管理系统的设计与实现.doc");

        //获取文件名
        String fileName = file.getName();
        System.out.println("文件名:" + fileName);

        //判断是否是一个目录
        System.out.println(file.isDirectory());//false

        //判断是否是一个文件
        System.out.println(file.isFile());//true

        //求最后一次修改文件的时间
        long time = file.lastModified();// 这个毫秒是从1970年到现在的总毫秒数。
        // 将总毫秒数转换成日期
        Date date = new Date(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss SSS");
        String nowtime = sdf.format(date);
        System.out.println(nowtime + " " +  file.getName());
    }
}

运行结果:

在这里插入图片描述

File中的listFiles方法。

示例代码03:

public class FileTest03 {
    public static void main(String[] args) {
        // File[] listFiles()
        // 获取当前目录下所有的子文件。
        File f = new File("D:\Markdown");
        File[] files = f.listFiles();
        // foreach
        for(File file : files){
            //System.out.println(file.getAbsolutePath());
            System.out.println(file.getName());
        }
    }
}

运行结果:

在这里插入图片描述

使用File类进行目录拷贝

示例代码04:

public class CopyAll {
    public static void main(String[] args) {
        //拷贝源
        File srcFile = new File("E:\\JavaWeb\\技术文档课件");

        //拷贝目标
        File destFile = new File("F:\\");

        //调用拷贝方法开始拷贝
        copyDir(srcFile,destFile);
    }

    /**
     * 拷贝目录
     * @param srcFile 拷贝源
     * @param destFile 拷贝目标
     */
    public static void copyDir(File srcFile, File destFile) {
        //如果拷贝的是文件,递归停止
        if(srcFile.isFile()){
            //SrcFile如果是一个文件的话,递归结束
            //是文件的时候需要拷贝
            //一边读一边写
            FileInputStream in = null;
            FileOutputStream out = null;

            try {
                // 读这个文件
                // E:\JavaWeb\技术文档课件\[尚硅谷]_许刚_javaweb讲义.docx.com
                in = new FileInputStream(srcFile);
                // 写到这个文件中
                // F:\JavaWeb\技术文档课件\[尚硅谷]_许刚_javaweb讲义.docx.com
                String path = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\")  + srcFile.getAbsolutePath().substring(3);
                out = new FileOutputStream(path);
                // 一边读一边写
                byte[] bytes = new byte[1024 * 1024]; // 一次复制1MB
                int readCount = 0;
                while((readCount = in.read(bytes)) != -1){
                    out.write(bytes, 0, readCount);
                }
                out.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if(out != null){
                    try {
                        out.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(in != null){
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return;//出错点
        }

        //获取源文件下的子目录
        File[] files = srcFile.listFiles();//出错点
        for(File file : files){//出错点
            // 获取所有文件的(包括目录和文件)绝对路径
            //System.out.println(file.getAbsolutePath());
            if(file.isDirectory()){//出错点
                // 新建对应的目录
                //System.out.println(file.getAbsolutePath());
                //E:\JavaWeb\技术文档课件\[尚硅谷]_许刚_javaweb讲义.docx.com       源目录
                //F:\JavaWeb\技术文档课件\[尚硅谷]_许刚_javaweb讲义.docx.com       目标目录
                String srcDir = file.getAbsolutePath();
                String destDir = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : destFile.getAbsolutePath() + "\\")  + srcDir.substring(3);
                File newFile = new File(destDir);
                if(!newFile.exists()){
                    newFile.mkdirs();
                }
            }
            // 递归调用
            copyDir(file, destFile);//出错点

        }
    }
}

拷贝结果:

在这里插入图片描述

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94255.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!