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();
        }
    
    }
    
  • 相关阅读:
    IntelliJ idea 2021.3 安装使用及激活
    高项-信息系统基础-UML图
    软考高项(信息系统项目管理师)介绍
    Android Studio中的Gradle面板没有Task任务列表如何找回?
    ubuntu 安装nodejs,npm,
    解决video.js video-player不能自动播放问题
    vuex开启namespaced
    axios提交body raw格式
    vue配置服务代理
    PIDFile没有配置导致将mongodb配置成服务时启动失败
  • 原文地址:https://www.cnblogs.com/lcj0703/p/7772983.html
Copyright © 2011-2022 走看看