java io 缓冲流_记忆系列-Java IO的缓存输入输出流(高效流)

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();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

StringdestFile="D:"+File.separator+"destfile.txt";

StringlineSeparator=System.getProperty("line.separator");

//字节缓冲输出流

try(FileOutputStreamfos=newFileOutputStream(destFile,true);

BufferedOutputStreambos=newBufferedOutputStream(fos)){

bos.write(lineSeparator.getBytes());

bos.write("hello FileOutputStream".getBytes());

//使用try-with-resources优雅关闭资源(https://199604.com/1241)

//bos.close();

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

//字节缓冲输入流

try{

FileInputStreamfis=newFileInputStream(destFile);

BufferedInputStreambis=newBufferedInputStream(fis);

byte[]buffer=newbyte[1024];

intlen=-1;

while((len=bis.read(buffer))!=-1){

System.out.println(newString(buffer,0,len));

}

fis.close();

bis.close();

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione1){

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();

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

tringdestFile="D:"+File.separator+"destfile.txt";

StringlineSeparator=System.getProperty("line.separator");

//字符输出流

try(BufferedWriterout=newBufferedWriter(newFileWriter(destFile,true))){

out.write("郭大傻?");

out.write(lineSeparator);

out.write("结束~");

//使用try-with-resources优雅关闭资源(https://199604.com/1241)

//out.close();

}catch(IOExceptione){

e.printStackTrace();

}

//字符输入流

try(BufferedReaderin=newBufferedReader(newFileReader(destFile))){

Stringline=null;

while((line=in.readLine())!=null){

System.out.println(line);

}

}catch(FileNotFoundExceptione){

e.printStackTrace();

}catch(IOExceptione){

e.printStackTrace();

}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值