栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

缓冲字节流

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

缓冲字节流

缓冲字节流
public class Test01 {
    public static void main(String[] args) {
        try {
           // Test01.testBufferedInputStream();
        } catch (Exception e) {
            e.printStackTrace();
        }
        //Test01.testBufferedOutputStream();
        Test01.copyFile();
    }

    
    public static void testBufferedInputStream() throws Exception {
        //文件字节流输入对象
        FileInputStream in = new FileInputStream("E:\java\idea\javaBasics\javaSE\基础语法\src\ioStream\Buffered\tt.txt");
        //把文件字节输入流放入缓冲字节流输入对象
        BufferedInputStream bi = new BufferedInputStream(in);
        byte[] bytes = new byte[1024];
        int len=0;
        while ((len=bi.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        //关闭流,先进后出
        bi.close();
        in.close();
    }

    
    public static void testBufferedOutputStream(){
        try {
            //创建字节输出流对象
            FileOutputStream out = new FileOutputStream("E:\java\idea\javaBasics\javaSE\基础语法\src\ioStream\Buffered\tt1.txt");
            //把字节输出流对象放入缓冲字节输出流对象
            BufferedOutputStream bo = new BufferedOutputStream(out);
            String str="hello";
            bo.write(str.getBytes());//写入内存中
            bo.flush();//把内存中的文件刷到硬盘中
            bo.close();
            out.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    
    public static void copyFile(){
        try {
            //缓冲输入流
            BufferedInputStream in = new BufferedInputStream(new FileInputStream("E:\java\idea\javaBasics\javaSE\基础语法\src\ioStream\Buffered\tt1.txt"));
            //缓冲输出流
            BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("E:\java\idea\javaBasics\javaSE\基础语法\src\ioStream\Buffered\tt2.txt"));
            byte[] bytes = new byte[1024];
            int len=0;//设置一个每次读取到的数据的长度,直到in.read执行到最后(比如说文件中只有一个hello,所谓的执行到最后一个就是读取o的后面,这个时候返回值是-1)
            while ((len=in.read(bytes))!=-1){
                out.write(bytes,0,len);//写到内存中
            }
            out.flush();//刷到硬盘
            out.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/282657.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号