zoukankan      html  css  js  c++  java
  • 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毫秒

  • 相关阅读:
    随便说说
    郁闷
    请各栏目的负责人,开始整理自己栏目的文章
    祝博客园生日快乐
    Windows Live Writer中打开博客日志(最新版可以支持打开3000以内的日志)
    编译器优化对齐(字节对齐)
    HDlock 锁住硬盘的解决方式
    linux中env,export, set的区别
    System Volume Information 文件夹权限控制
    BOOL与bool的区别(bool不是c的关键字,c++中bool也不是int)
  • 原文地址:https://www.cnblogs.com/xiaopangyu/p/9268636.html
Copyright © 2011-2022 走看看