IO使用

转载

1. 字节流复制文件

package io2;

import java.io.*;

/**
 *
 * 测试复制的时间
 * Create by stefan
 * Date on 2018-05-28  10:28
 * Convertion over Configuration!
 */
public class copy2 {
    //一个字节一个字节的复制,耗时22697毫秒
    public static  void  fun() throws IOException {
        FileInputStream fis = new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf");
        FileOutputStream fos = new FileOutputStream("E:\\modern-java.pdf");
        int by = 0;
        while ((by=fis.read()) != -1) {
            fos.write(by);
            fos.flush();
        }
        fis.close();
        fos.close();
    }
    //1024字节数组复制 耗时63毫秒
    public  static void  fun1() throws IOException {
        FileInputStream fis = new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf");
        FileOutputStream fos = new FileOutputStream("E:\\modern-java.pdf");
        int len = 0;
        byte[] bytes =new byte[1024];
        while ((len=fis.read(bytes)) != -1) {
            fos.write(bytes,0,len);
            fos.flush();
        }
        fis.close();
        fos.close();
    }
    // 一个字节一个字节复制,但是用了缓冲流 耗时64毫秒
    public static   void  fun2() throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\modern-java.pdf"));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf"));
        int by = 0;
        while ((by=bis.read()) != -1) {
            bos.write(by);
            bos.flush();
        }
        bis.close();
        bos.close();
    }
    // 1024字节数组复制并用了缓冲流 耗时7毫秒
    public  static void  fun3() throws IOException {
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("E:\\modern-java.pdf"));
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\汤包\\慕课大巴\\modern-java.pdf"));
        int len = 0;
        byte[] bytes =new byte[1024];
        while ((len=bis.read(bytes)) != -1) {
            bos.write(bytes,0,len);
            bos.flush();
        }
        bis.close();
        bos.close();
    }

    public static void main(String args[]) throws IOException {
        long t1 = System.currentTimeMillis();
        fun3();
        long t2 = System.currentTimeMillis();
        System.out.println(t2-t1);
    }

}

2. 复制单级文件夹

/**
 * 封装
 * 新建文件夹
 * 获得源文件夹下文件列表
 * 复制文件到新文件夹
 */
public class copyFolder {
    public static void main(String args[]) throws IOException {
        File file1 = new File("F:\\汤包\\IT时代\\java基础\\day21\\code\\demo");
        File file2 = new File("e:\\demo");
        //判断文件夹是否存在
        if (!file2.exists()){
            file2.mkdir();
        }

        //获取源文件夹下文件列表

        File[] files = file1.listFiles();
        for (File file : files) {
            File newfile = new File(file2,file.getName());
            copyFun(file,newfile);
        }
    }

    private static void copyFun(File file1,File file2) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes,0,len);
        }
        bis.close();
        bos.close();
    }
}

3. 复制多级文件夹

使用递归实现

/*
 * 需求:复制多极文件夹
 *
 * 数据源:F:\汤包\IT时代\java基础\day21\code\demos
 * 目的地:E:\\
 *
 * 分析:
 *      A:封装数据源File
 *      B:封装目的地File
 *      C:判断该File是文件夹还是文件
 *          a:是文件夹
 *              就在目的地目录下创建该文件夹
 *              获取该File对象下的所有文件或者文件夹File对象
 *              遍历得到每一个File对象
 *              回到C
 *          b:是文件
 *              就复制(字节流)
 */

public class copyFolder2 {

    public static void main(String args[]) throws IOException {
        File file1 = new File("F:\\汤包\\IT时代\\java基础\\day21\\code\\demos");
        File file2 = new File("e:\\");
        copyFolder(file1,file2);
    }

    private static void copyFolder(File srcFile, File destFile) throws IOException {
        if (srcFile.isDirectory()){
            File newFolder = new File(destFile, srcFile.getName());
            newFolder.mkdir();
            File[] files = srcFile.listFiles();
            for (File file1 : files) {
                copyFolder(file1,newFolder);
            }
        }else {
            File newFile = new File(destFile,srcFile.getName());
            copyFile(srcFile,newFile);
        }

    }

    private static void copyFile(File file1,File file2) throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file2));
        byte[] bytes = new byte[1024];
        int len = 0;
        while ((len = bis.read(bytes)) != -1) {
            bos.write(bytes,0,len);
        }
        bis.close();
        bos.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值