记忆系列-Java IO的缓存输入输出流(高效流)
本文最后更新于 2543 天前,其中的信息可能已经有所发展或是发生改变。

2019-09-02今天是周一,应该是学校升国旗开学典礼的一天吧~

然而垃圾的我还是没有找到工作,大专狗是真的没用~简历也不会看你的呀~


在之前我自己也mark了字节和字符的流,但是呢读取数据量比较大时,速度是8是会有点慢?影响到你郭良俊自己的脾气呢?

好了~那就在内存/不怕被坑情况下自己弄一下呗~好了马记录起~

缓冲流,根据流的分类:字节缓冲流和字符缓冲流。目的可能提高IO流的读写速度,至于利弊嘛–>出门右拐–>百度/谷歌走起~

字节缓冲流:BufferedInputStream、BufferedOutputStream

字符缓冲流:BufferedReader、BufferedWriter

它们的内部都包含了一个缓冲区,通过缓冲区读写,就可以提高了IO流的读写速度,做到缓存的作用~

字节缓冲流例子

String destFile = "D:"+File.separator+"destfile.txt";
        String lineSeparator = System.getProperty("line.separator");
        //字节缓冲输出流
        try(FileOutputStream fos = new FileOutputStream(destFile,true);
            BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            bos.write(lineSeparator.getBytes());
            bos.write("hello FileOutputStream".getBytes());
            //使用try-with-resources优雅关闭资源(https://199604.com/1241)
            //bos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //字节缓冲输入流

        try {
            FileInputStream fis = new FileInputStream(destFile);
            BufferedInputStream bis = new BufferedInputStream(fis);
            byte[] buffer = new byte[1024];
            int len = -1;
            while ((len = bis.read(buffer))!=-1){
                System.out.println(new String(buffer,0,len));
            }
            fis.close();
            bis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e1){
            e1.printStackTrace();
        }

字符缓冲流例子

tring destFile = "D:"+File.separator+"destfile.txt";
        String lineSeparator = System.getProperty("line.separator");
        //字符输出流
        try(BufferedWriter out = new BufferedWriter(new FileWriter(destFile,true))){
            out.write("郭大傻?");
            out.write(lineSeparator);
            out.write("结束~");
            //使用try-with-resources优雅关闭资源(https://199604.com/1241)
            //out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //字符输入流
        try(BufferedReader in = new BufferedReader(new FileReader(destFile))) {
            String line = null;
            while ((line= in.readLine())!=null){
                System.out.println(line);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

一位辣鸡的菜鸡mark结束系列~


Image
Image

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Image
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇