字节缓冲流
BufferedInputStream&BufferedOutputStream
装饰流也就是处理流的一种
最底层还是节点流
作用:提高了我们操作的性能。
IO的操作是我们影响程序性能的一个瓶颈,所以内部维护了一个缓冲区,提高我们读写的操作,减少了读写的次数
释放时只需要释放最外层的处理流,内部会自动找到节点流释放
套在外面就可以了: is =new BufferedInputStream( new FileInputStream(src));
文件字节输入流外套一个处理缓冲流
package com.sxt.io;
/**
* 文件字节输入流
* 四个步骤:分段读取 加入缓冲流
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放资源
*/
import java.io.*;
public class BufferedTest01 {
public static void main(String[] args) {
//创建源
File src = new File("abc.txt");
//选择流
InputStream is=null;
try {
is =new BufferedInputStream( new FileInputStream(src));
//操作(分段读取)
byte[] flush =new byte[1024];//缓冲容器
int len=-1;//接受长度
while ((len =is.read(flush))!=-1){
//字节数组----->字符串(解码)
String str=new String(flush,0,len,"UTF-8");
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//释放资源
try {
if (null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void test1(){
//创建源
File src = new File("abc.txt");
//选择流
InputStream is=null;
BufferedInputStream bis=null;
try {
is = new FileInputStream(src);
bis=new BufferedInputStream(is);
//操作(分段读取)
byte[] flush =new byte[1024];//缓冲容器
int len=-1;//接受长度
while ((len =is.read(flush))!=-1){
//字节数组----->字符串(解码)
String str=new String(flush,0,len,"UTF-8");
System.out.print(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
//释放资源
try {
if (null!=is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (null!=bis) {
bis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
文件字节输入流外套一个处理缓冲流
外套一个缓冲流: os = new BufferedOutputStream(new FileOutputStream(dest,true));
package com.sxt.io;
import java.io.*;
/**
* 文件字节输出流 放入缓冲流
* 1,创建源
* 2.选择流
* 3.操作
* 4.释放资源
*/
public class BufferedTest02 {
public static void main(String[] args) {
//1、创建源
File dest = new File("dest.txt");
//2、选择流
OutputStream os=null;
try {
os = new BufferedOutputStream(new FileOutputStream(dest,true));
//3、操作(写出)
String msg="IO is so easy";
byte[] datas=msg.getBytes();//字符串----->字节数组(编码)
os.write(datas,0,datas.length);
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
拷贝一份大内存的视频对比加入缓冲流很没加缓冲流的运行时间
可以明显发现加入缓冲流的运行时间短了很多,性能有的很大的提升
注意:嵌套缓冲流只能性能提升一次,嵌套多次并无意义
package com.sxt.io;
/**
* 文件拷贝:文件字节输入、输出流
*/
import java.io.*;
public class Copy01 {
public static void main(String[] args) {
long t1=System.currentTimeMillis();
copy("src/com/sxt/io/Copy01.java", "copy.txt");
long t2=System.currentTimeMillis();
System.out.println(t2-t1);
}
public static void copy(String srcPath, String destPath) {
//1、创建源
File src = new File(srcPath);//源头
File dest = new File(destPath);//目的地
//2、选择流
try (InputStream is = new BufferedInputStream(new FileInputStream(src));
OutputStream os = new BufferedOutputStream(new FileOutputStream(dest));) {
//操作(分段读取)
byte[] flush = new byte[1024];//缓冲容器
int len = -1;//接受长度
while ((len = is.read(flush)) != -1) {
//分段写出
os.write(flush, 0, len);
}
os.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符缓冲流
BufferedReader&BufferedWriter
处理的是字符也就是文本
新增许多方法
尽量不要发生多态
字符输入缓冲流
定义一个接收字符串,接收到了打印内容,没接收到就等于空
package com.sxt.io;
/**
* 文件字符输入流
* 四个步骤:分段读取 加入缓冲流
* 1.创建源
* 2.选择流
* 3.操作
* 4.释放资源
*/
import java.io.*;
public class BufferedTest03 {
public static void main(String[] args) {
//创建源
File src = new File("abc.txt");
//选择流
BufferedReader reader=null;
try {
reader = new BufferedReader(new FileReader(src));
//操作(分段读取)
String line=null;
while ((line=reader.readLine())!=null){
//字符数组----->字符串
System.out.println(line); }
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
//4、释放资源
if (null!=reader)
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
新增newLine()方法代替\t,\r换行
writer.append(“IO is so easy!”);
writer.newLine();
writer.append(“单清宝will成功!”);
package com.sxt.io;
/**
* 文件字符输出流 加入缓冲流
* 1,创建源
* 2.选择流
* 3.操作
* 4.释放资源
*/
import java.io.*;
public class BufferedTest04 {
public static void main(String[] args) {
//1、创建源
File dest = new File("dest.txt");
//2、选择流
BufferedWriter writer=null;
try {
writer =new BufferedWriter(new FileWriter(dest)) ;
//3、操作(写出)
// 方法一
// String msg="单清宝will成功!";
// char[] datas=msg.toCharArray();//字符串----->字符数组(编码)
// writer.write(datas,0,datas.length);
// writer.flush();
//方法二
// String msg="IO is so easy!\n单清宝will成功!";
// writer.write(msg);
// writer.flush();
//方法三
writer.append("IO is so easy!");
writer.newLine();
writer.append("单清宝will成功!");
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
纯文本的拷贝加入缓冲流
逐行读取逐行写出
换行
package com.sxt.io;
/**
* 文件拷贝:文件字节输入、输出流
*/
import java.io.*;
public class Copy02 {
public static void main(String[] args) {
copy("abc.txt", "abc-copy.txt");
}
public static void copy(String srcPath, String destPath) {
//1、创建源
File src = new File(srcPath);//源头
File dest = new File(destPath);//目的地
//2、选择流
try (BufferedReader br = new BufferedReader(new FileReader(src));
BufferedWriter bw = new BufferedWriter(new FileWriter(dest));) {
//操作(逐行读取)
String line=null;
while ((line=br.readLine()) != null) {
//逐行写出
bw.write(line);
bw.newLine();
}
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}