zoukankan      html  css  js  c++  java
  • IO流(字节流复制)01

    package ioDemo;
    
    import java.io.*;
    
    /**
     * IO流(字节流复制)
     * Created by lcj on 2017/11/2.
     */
    public class bufferReadWrintDemo {
        //声明异常
        public static void main(String[] args) throws IOException {
    //        copy01();
            copy02();
    //        copy03();
    //        copy04();
        }
        //自定义缓冲区
        public static  void copy01() throws IOException
        {
            FileInputStream fileInputStream = new FileInputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\2.txt");
            FileOutputStream  fileOutputStream= new FileOutputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\1.txt");
            //声明数组大小
            byte[] buf01 = new byte[1024];
            int len01 = 0 ;
            while ((len01 = fileInputStream.read(buf01) )!= -1)
            {
                fileOutputStream.write(buf01,0,len01);//将buf01中数据写入至len01中
            }
        }
        //自定义缓冲区方法BufferedInputStream
        public static  void copy02() throws  IOException
        {
            FileInputStream fileInputStream = new FileInputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\2.txt");
            BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
    
            FileOutputStream  fileOutputStream= new FileOutputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\22.txt");
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
    
            byte[] buf02 = new byte[1024];
            int len02 = 0 ;
            while ((len02 = bufferedInputStream.read(buf02)) != -1)
            {
                bufferedOutputStream.write(buf02,0,len02);
                bufferedOutputStream.flush();  //刷新缓冲区数据
            }
            bufferedInputStream.close();
            bufferedOutputStream.close();
        }
        //不建议使用此方法,此方法按照字节读取,效率低
        public static void copy03() throws IOException
        {
            FileInputStream fileInputStream = new FileInputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\2.txt");
            FileOutputStream  fileOutputStream= new FileOutputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\3.txt");
    
            int len03 = 0;
            while ((len03 = fileInputStream.read()) != -1)
            {
                fileOutputStream.write(len03);
            }
            fileInputStream.close();
            fileOutputStream.close();
        }
        ///
        public static  void copy04()  throws IOException
        {
            FileInputStream fileInputStream = new FileInputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\2.txt");
            FileOutputStream  fileOutputStream= new FileOutputStream("D:\Project\IdeaProjects\test01_time\src\main\java\ioDemo\4.txt");
    
            byte[] buf = new byte[1024];
            int len = 0;
            while ((len = fileInputStream.read(buf)) != -1)
            {
                fileOutputStream.write(buf, 0, len);
    //            System.out.println(new String(buf ,0 ,len));
            }
            fileOutputStream.close();
            fileInputStream.close();
        }
    
    }
    
  • 相关阅读:
    [转]实习生需要懂的40大基本规矩
    [转]Linux下pppoe配合Drcom插件上网方法介绍......
    收藏一些图书
    [转]30个自我提升技巧
    [转]关于Gmail打不开的解决办法
    [转]李开复经典语录盘点:人生之路在于每次的选择
    [转]哈佛管理世界中智慧
    胡伟武校友在2011年中国科大本科生毕业典礼暨学位授予仪式上的讲话
    Dynamics4.0和Dynamics2011处理Email的方法
    JS实现简单的ToolTip功能
  • 原文地址:https://www.cnblogs.com/lcj0703/p/7772983.html
Copyright © 2011-2022 走看看