美文网首页
IO高效缓冲区复制文件:

IO高效缓冲区复制文件:

作者: 公子请留步LookMe | 来源:发表于2018-08-25 00:09 被阅读0次

例题:
检验高效缓冲区与普通的快慢.

package demoByte;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class BufferedCopy2 {
    public static void main(String[] args) {
        long starTime = System.currentTimeMillis();
        copy();
        long endTime = System.currentTimeMillis();
        System.out.println("耗时"+(endTime-starTime)+"毫秒");
        
    }
    /**
     * 使用单个字节的形式
     */
    public static void copy() {
        InputStream in = null;
        OutputStream op = null;
        try {
            in = new  FileInputStream("/CODE/JAVA/eclipse/NEW/IO-Demo2/src/demoByte/InputSteam1.java");
            op = new FileOutputStream("InputSteam1.txt");
            int len = -1;
            while((len = in.read())!= -1) {
                op.write(len);  
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(op != null) {
                    op.close();
                }
                if(in != null)
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    /**
     * 使用数组字节的形式
     */
    public static void copy2() {
        InputStream in = null;
        OutputStream op = null;
        try {
            in = new  FileInputStream("/CODE/JAVA/eclipse/NEW/IO-Demo2/src/demoByte/InputSteam1.java");
            op = new FileOutputStream("InputSteam1.txt");
            byte[] by = new byte[1024];
            int len = -1;
            while((len = in.read(by))!= -1) {
                op.write(by,0,len); 
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(op != null) {
                    op.close();
                }
                if(in != null)
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    /**
     * 使用高效缓冲区单个字节的形式
     */
    public static void copy3() {
        BufferedInputStream in = null;
        BufferedOutputStream op = null;
        try {
            in =new BufferedInputStream( new  FileInputStream("/CODE/JAVA/eclipse/NEW/IO-Demo2/src/demoByte/InputSteam1.java"));
            op = new BufferedOutputStream(new FileOutputStream("InputSteam1.txt"));

            int len = -1;
            while((len = in.read())!= -1) {
                op.write(len);  
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(op != null) {
                    op.close();
                }
                if(in != null)
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
    /**
     * 使用高效缓冲区复制字节数组的形式
     */
    public static void copy4() {
        BufferedInputStream in = null;
        BufferedOutputStream op = null;
        try {
            in =new BufferedInputStream( new  FileInputStream("/CODE/JAVA/eclipse/NEW/IO-Demo2/src/demoByte/InputSteam1.java"));
            op = new BufferedOutputStream(new FileOutputStream("InputSteam1.txt"));
            byte[] by = new byte[1024];
            int len = -1;
            while((len = in.read(by))!= -1) {
                op.write(by,0,len); 
            }
            
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch(IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(op != null) {
                    op.close();
                }
                if(in != null)
                in.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        
    }
}

相关文章

  • IO高效缓冲区复制文件:

    例题:检验高效缓冲区与普通的快慢.

  • IO模型

    IO读写 read:把数据从内核进程复制到进程缓冲区 write:把数据从进程缓冲区复制到内核缓冲区 阻塞IO 需...

  • Java IO流之拷贝(复制)文件

    方式一(字符流读写复制文件,仅限文本文件) 方式二(字符流缓冲区读写文件-高效,仅限文本文件) 方式三(字节流读写...

  • IO流学习总结-字符流

    字符流 复制文件1 通过FileReader和FileWriter实现; 复制文件2 通过缓冲区BufferedR...

  • 【高性能网络编程】基础知识2 IO多路复用与Reactor 模式

    对比几种不同的 IO 阻塞IO应用进程被阻塞,直到数据从内核缓冲区复制到应用进程缓冲区中才返回。特点:阻塞进程,C...

  • 文件io复制

    比较经典的一个代码,使用FileInputStream读取文件A的字节,使用FileOutputStream写入到...

  • 2020-03-13-Java

    1.BufferedInputStream和BufferedOutputStream (1)没有缓冲区,去复制文件...

  • IO流——字节流4种copy方式

    JAVA基本IO流框架 字节流整体可分为带缓冲区的流和不带缓冲区的流可分为逐字节复制的流和逐块复制的流(块其实就是...

  • NIO复制文件的三种方式对比

    方式一:利用通道完成文件的复制(非直接缓冲区) 运行三次的时间分别为:用通道完成文件的复制(非直接缓冲区)耗时为1...

  • Java知识梳理四

    一、Java中的文件复制 1.Java IO实现文件复制 利用java.io类库,直接为源文件构建一个Fil...

网友评论

      本文标题:IO高效缓冲区复制文件:

      本文链接:https://www.haomeiwen.com/subject/mlexiftx.html