• java通过IO流复制文件


    package kimoji;

    import java.io.*;

    public class FileTest {

    public static void main(String[] args) throws IOException {
    out1("D:\ppt\Oracle_SQL基礎知識.ppt", "aaa.ppt");
    out2("D:\ppt\Oracle_SQL基礎知識.ppt", "aaa1.ppt");
    out3("D:\ppt\Oracle_SQL基礎知識.ppt", "aaa11.ppt");
    out4("D:\ppt\Oracle_SQL基礎知識.ppt", "aaa111.ppt");


    /*if (file.exists() && file.isDirectory()) {
    String names[] = file.list();
    for (String name : names) {
    System.out.println(name);
    }
    System.out.println("000000000000000000000000000000000000000000000000000000000000000000000000");
    String[] nameNei = file.list(new FilenameFilter() {

    @Override
    public boolean accept(File dir, String name) {
    return name.endsWith(".java");
    }
    });
    for (String string : nameNei) {
    System.out.println(string);
    }
    }*/


    }

    /**
    * 一次读取一个字节
    * @param src1 :读取路径
    * @param src2 :写入路径
    * @throws IOException
    */
    public static void out1(String src1,String src2) throws IOException{
    FileInputStream fileInputStream = new FileInputStream(src1);
    FileOutputStream fOutputStream = new FileOutputStream(src2);
    int len = -1;
    Long star = System.currentTimeMillis();
    while((len = fileInputStream.read())!=-1){
    fOutputStream.write(len);
    }
    long end = System.currentTimeMillis();
    System.out.println("out1耗时为:"+(end-star)+"毫秒");
    fileInputStream.close();
    fOutputStream.close();
    }

    /**
    * 一次读取一个字节数组
    * @param str1:读取路径
    * @param str2:写入路径
    * @throws IOException
    */
    public static void out2(String str1,String str2) throws IOException{
    FileInputStream fis = new FileInputStream(str1);
    FileOutputStream fos = new FileOutputStream(str2);
    int len = -1;
    byte temp [] = new byte[1024];
    Long star = System.currentTimeMillis();
    while((len = fis.read())!=-1){
    fos.write(temp, 0, len);
    }
    long end = System.currentTimeMillis();
    System.out.println("out2耗时为:"+(end-star)+"毫秒");
    fis.close();
    fos.close();
    }
    /**
    * 带有缓冲区的一次读取一个字节
    * @param src1 :读取路径
    * @param src2 :写入路径
    * @throws IOException
    */
    public static void out3(String str1,String str2) throws IOException{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str1));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(str2));
    int len = -1;
    Long star = System.currentTimeMillis();
    while((len=bis.read())!=-1){
    bos.write(len);
    }
    Long end = System.currentTimeMillis();
    System.out.println("out3共耗时"+(end-star)+"毫秒");
    bis.close();
    bos.close();
    }
    public static void out4(String str1,String str2) throws IOException{
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(str1));
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(str2));
    int len = -1;
    byte [] temp = new byte[1024];
    Long star = System.currentTimeMillis();
    while((len = bis.read())!=-1){
    bos.write(temp, 0, len);
    }
    Long end = System.currentTimeMillis();
    System.out.println("out4共耗时"+(end-star)+"毫秒");
    bis.close();
    bos.close();
    }
    }

     用韵结果如下:

    out1耗时为:6431毫秒
    out2耗时为:6223毫秒
    out3共耗时28毫秒
    out4共耗时197毫秒

  • 相关阅读:
    Gitlab 自带Nginx与原Nginx冲突的解决方案
    宝塔PHP7.3版本安装ZIP扩展
    GitLab配置ssh key
    Git Windows打分支 并推送到远程
    proj4 coordinates must be finite numbers
    uniapp APP端水印相机实现
    uniapp使用逆地理编码
    uniapp sqlite数据库使用
    uniapp APP 定位获取
    uniapp打包流程
  • 原文地址:https://www.cnblogs.com/xiaopangyu/p/9268636.html
Copyright © 2020-2023  润新知