javase-io-210509-02

javase-io-210509-02

  • File

FIle

Fiel
	1、 File类和四大家族没有关系,所以FIle类不能完成对文件的读和写
	2、 FIle对象代表  文件和目录路径名的抽象表示形式
		C:\bgy				这是一个文件
		C:\bgy\bgy.txt		 这也是一个文件
Demo01.java
import java.io.File;
import java.io.IOException;

public class Demo01 {
    public static void main(String[] args) {

        File file01 = new File("log.txt");
        File file02 = new File("testFile02");

        // 判断文件是否存在
        System.out.println(file01.exists());

        // 如果文件file不存在,以 文件 的形式创建
        if (!file01.exists()){
            try {
                file01.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        // 如果文件file不存在,以 目录 的形式创建
        if (!file02.exists()){
             file02.mkdir();
        }

        // 可以创建多重目录
        File file03 = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509\\a\\b\\c\\d");
        if (!file03.exists()) {
            file03.mkdirs();
        }

        File file04 = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509\\log.txt");
        // 获取文件父路径
        String parentPath = file04.getParent();
        System.out.println(parentPath);
        // 获取父文件
        File parentFile = file04.getParentFile();
        System.out.println("绝对路径:"+parentFile.getAbsolutePath());

        File file05 = new File("myfile");
        System.out.println("myfile绝对路径:"+file05.getAbsolutePath());
    }
}
Demo02.java
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Demo02 {
    public static void main(String[] args) {

        File file01 = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509\\log.txt");
        // 获取文件名
        System.out.println("文件名:"+file01.getName());

        // 判断是否为一个目录
        System.out.println(file01.isDirectory());
        // 判断是否为一个文件
        System.out.println(file01.isFile());

        // 获取文件最后一次修改时间,,这毫秒是从1970年到现在总毫秒数
        long haomiao = file01.lastModified();
        System.out.println(haomiao);
        // 将总毫秒数转换成日期
        Date time = new Date(haomiao);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
        String strTime = sdf.format(time);
        System.out.println(strTime);

        // 获取文件大小
        System.out.println(file01.length());

    }
}
Demo03.java
package com.bgy07;

import java.io.File;

public class Demo03 {
    public static void main(String[] args) {

        File f = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509");

        // f.listFiles()  获取当前目录下的所有子文件
        File[] files = f.listFiles();

        for (File file : files){
//            System.out.println(file.getAbsolutePath());
            System.out.println(file.getName());
        }
    }
}
拷贝目录,文件
package com.bgy07;

import java.io.*;

public class Copy {
    public static void main(String[] args) {

        // 拷贝源
        File srcFile = new File("G:\\warehouse\\coding\\javase-io\\javase-io-210509");

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

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

    /**
     *
     * @param srcFile       拷贝源
     * @param destFile      拷贝目标
     */
    private static void copyDir(File srcFile, File destFile) {

        // srcFile是文件的话就一边读一边写
        // 就需要拷贝
        // 递归结束
        if (srcFile.isFile()){
            FileInputStream in = null;
            FileOutputStream out = null;

            // 测试路径
//            String srcPath = srcFile.getAbsolutePath();
//            System.out.println(srcPath);
//            String destPath = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : ( destFile.getAbsolutePath() + "\\" ))+ srcPath.substring(3);
//            System.out.println(destPath);

            try {

                // 获取源路径和目标路径
                String srcPath = srcFile.getAbsolutePath();
                String destPath = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : ( destFile.getAbsolutePath() + "\\" ))+ srcPath.substring(3);

                in = new FileInputStream(srcPath);
                out = new FileOutputStream(destPath);

                byte[] bytes = new byte[1024*1024];
                // 记录一次读到多少个
                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());

            // 判断file是否为目录 , 如果是目录就新建目录
            if (file.isDirectory()) {
                String srcDir = file.getAbsolutePath();

                // 如果目标路径不是以 “ \\ ” 结尾,,,就加上去
                String destDir = (destFile.getAbsolutePath().endsWith("\\") ? destFile.getAbsolutePath() : ( destFile.getAbsolutePath() + "\\" ))+ srcDir.substring(3);
//                String destDir = destFile.getAbsolutePath() + srcDir.substring(3);
//                System.out.println(destDir);

                File newFile = new File(destDir);
                if (!newFile.exists()) {
                    newFile.mkdirs();
                }
            }

            // 递归调用
            copyDir(file,destFile);
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值