-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优雅关闭资源(/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优雅关闭资源(/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优雅关闭资源(/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优雅关闭资源(/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结束系列~