• 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();
        }
    
    }
    
  • 相关阅读:
    如何快速给pod添加健康检查?
    如何快速下载vagrant的box?
    如何快速新建sql的demo环境?
    如何快速将某个用户添加sudo免密以及docker权限?
    七、Docker+nginx
    六、Docker+Gitlab
    五、Docker+Sqlserver
    四、Docker+Tomcat
    Docker 内程序时间设置,很重要
    三、Docker镜像的相关操作
  • 原文地址:https://www.cnblogs.com/lcj0703/p/7772983.html
Copyright © 2020-2023  润新知