Java SE(6)

1. 使用File类相关的方法 ,编写删除文件夹的方法
要求:既能删除空文件夹 也能删除非空文件夹
public static void main(String[] args) throws IOException {

        File file=new File("D:\\testIO"); // 源文件路径
        File newFile=new File("C:\\");  // 目标路径
        newFile.mkdir();

        //  遍历原文键中的所有内容
        File[] files=file.listFiles();
        for (File f:files) {
            if(f.isFile()){ // 是文件

            }else{ // 是文件夹/目录
                f.mkdir();
            }
        }

    }

2. 使用绝对路径,在D盘创建一个testIO文件夹,然后再testIO文件中创建一个1.txt文件中;
使用相对路径,在当前项目下创建一个testIO文件夹,然后再testIO文件中创建一个2.txt文件中
public static void main(String[] args) throws IOException {


        File f=new File("D:\\testIO");
        System.out.println(f.exists());
        f.mkdir();
        File file=new File(f,"1.txt");
        System.out.println(file.exists());
        f.createNewFile();

        File file1=new File("testIO");
        f.mkdir();
        File file2=new File(file1,"2.txt");
        f.createNewFile();

    }
/**
*
3. 文件夹的复制
提示:结合 File类 和 字节输入输出流 相关的方法
*/
   public static void main(String[] args) throws IOException {
            //创建数据源File对象
            File srcFile = new File("D:\\testIO");
            //创建数据的目的对象
            File destFile = new File("C:\\");

            //写方法实现文件夹的复制,参数为数据源File对象和目的地File对象
            copyFolder(srcFile, destFile);

    }

    /**
     * 文件夹/文件的复制
     * @param srcFile  源文件夹得目录
     * @param destFile  地址文件夹的目录
     * @throws IOException
     */
    public static void copyFolder(File srcFile, File destFile) throws IOException {
        //判断 源File对象是否是目录/文件夹
        if (srcFile.isDirectory()) {
            String srcFileName = srcFile.getName();
            //在目的地下创建和 源File名称一样的 目录
            File newFolder = new File(destFile, srcFileName);
            if (!newFolder.exists()) {
                newFolder.mkdir();
            }

            //获取 源File下所有文件或者目录的 File数组
            File[] fileArray = srcFile.listFiles();
            //遍历该File数组,得到每一个File对象
            for (File file : fileArray) {
                //把该File作为数据源File对象,递归调用复制文件夹的方法
                copyFolder(file, newFolder);
            }

        } else {
            //不是,说明是文件,直接复制,采用字节流复制文件
            File newFile = new File(destFile, srcFile.getName());
            copyFile(srcFile, newFile);

        }
    }
    // 缓存区
    public static void copyFile(File srcFile, File destFile) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destFile));
        byte[] bytes = new byte[1024];
        int length=-1;
        while ((length = bis.read(bytes)) != -1) {
            bos.write(bytes, 0, length);
        }
        bis.close();
        bos.flush();
        bos.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值