Java进阶之旅第十二天
IO流
字节缓冲流
- 字节缓冲流,在原有的字节流中添加一个缓冲区,用于读取/写入提升效率
构造方法
构造方法 | 说明 |
---|
public BufferedInputStream(InputStream is) | 字节缓冲输入流,参数是字节输入流的类 |
public BufferedInputStream(InputStream is,int size) | 字节缓冲输入流,参数是字节输入流的类,size指定缓冲区大小 |
public BufferedOutputStream(OutputStream os) | 字节缓冲输出流,参数是字节输出流的类 |
public BufferedOutputStream(OutputStream os,int size) | 字节缓冲输出流,参数是字节输出流的类,size指定缓冲区大小 |
public static void main(String[] args) throws IOException {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src\\a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\\b.txt"));
int b;
while((b=bis.read())!=-1){
bos.write(b);
}
bos.close();
bis.close();
}
- 注意:
- 字节缓冲流中的读或写方法和参数中写入的对象的读或写方法是一致的
- 字符缓冲流提升效率的原理是,在内存中创建缓冲区,将读取的数据放入缓冲区中,从而减少从文件读取内容的时间,从而提升效率
字符缓冲流
构造方法
构造方法 | 说明 |
---|
public BufferedReader(Reader r) | 字符缓冲输入流,参数是字符输入流的类 |
public BufferedWriter(Writer r) | 字符缓冲输出流,参数是字符输出流的类 |
特有的方法
特有方法 | 说明 |
---|
public String readLine() | 读取一行数据,如果没有数据可读,就返回null |
public void newLine() | 跨平台的换行 |
练习
拷贝文件,比较用时(字节流)
import java.io.*;
import java.sql.Date;
import java.sql.Time;
public class Main {
public static void main(String[] args) throws IOException {
copy1("src\\all.png","src\\all1.png");
copy2("src\\all.png","src\\all2.png");
copy3("src\\all.png","src\\all3.png");
copy4("src\\all.png","src\\all4.png");
}
public static void copy1(String srcPath, String tarPath) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(tarPath);
int len;
while((len = fis.read())!=-1){
fos.write(len);
}
fos.close();
fis.close();
long end = System.currentTimeMillis();
System.out.printf("一次读写一个字节耗时%d\n",end-start);
}
public static void copy2(String srcPath, String tarPath) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(tarPath);
int len;
byte[] data = new byte[128];
while((len = fis.read(data))!=-1){
fos.write(data,0,len);
}
fos.close();
fis.close();
long end = System.currentTimeMillis();
System.out.printf("一次读写一个字节数组耗时%d\n",end-start);
}
public static void copy3(String srcPath, String tarPath) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(tarPath);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
while((len = bis.read())!=-1){
bos.write(len);
}
bos.close();
bis.close();
long end = System.currentTimeMillis();
System.out.printf("缓冲流: 一次读写一个字节耗时%d\n",end-start);
}
public static void copy4(String srcPath, String tarPath) throws IOException {
long start = System.currentTimeMillis();
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(tarPath);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int len;
byte[] data = new byte[128];
while((len = bis.read(data))!=-1){
bos.write(data,0,len);
}
bos.close();
bis.close();
long end = System.currentTimeMillis();
System.out.printf("缓冲流: 一次读写一个字节数组耗时%d\n",end-start);
}
}
一次读写一个字节耗时2223
一次读写一个字节数组耗时17
缓冲流: 一次读写一个字节耗时13
缓冲流: 一次读写一个字节数组耗时2