IO高效缓冲区复制文件:

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

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高效缓冲区复制文件:)