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();
        }
    
    }
    
  • 相关阅读:
    php大力力 [036节] 后台系统的登录页面界面做完啦
    php大力力 [035节] 先记录一些链接
    php大力力 [034节] 今天做出系统后台页面的界面啦
    php大力力 [033节] 随便看看:PHP程序员学习C++
    php大力力 [032节] php设计时候遇见麻烦:XQB50-H8268 进水电磁阀
    php大力力 [031节] php设计系统后台菜单和样式设计
    php大力力 [030节] php设计系统后台菜单
    php大力力 [029节] 做PHP项目如何下载js文件:使用腾讯浏览器把网上案例页面存储到本地
    php大力力 [027节] 被百度收录较好的几个视频网站示例
    SQL Server 创建触发器(trigger)---转载
  • 原文地址:https://www.cnblogs.com/lcj0703/p/7772983.html
Copyright © 2011-2022 走看看