BufferedInputStream 和BufferedOutputStre字节缓冲流
测试未加缓冲流和加缓冲流在速度上的差异,拷贝870M大小的文件,
package com.mike.io.eg;
import java.io.IOException;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.File;
/**
* @author mike
* @date 2020-11-26
* @desc 字节输入缓冲流,测试无缓冲流拷贝文件
*/
public class BufferedInputStreamDemo {
public static void main(String[] args) {
//定义输入流
FileInputStream fis = null;
FileOutputStream fos = null;
File source = new File("D:\\系统镜像\\ubuntu-18.04.4-live-server-amd64.iso");
File target = new File("F:\\ubuntu-18.04.4-live-server-amd64.iso");
try {
long start = System.currentTimeMillis();
fis = new FileInputStream(source);
fos = new FileOutputStream(target);
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) != -1) {
fos.write(buffer,0,len);
}
long end = System.currentTimeMillis();
System.out.println("拷贝成功,一共耗时" + (end - start) / 60 + "s" );//74s
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果
package com.mike.io.eg;
import java.io.IOException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
/**
* @author mike
* @date 2020-11-26
* @desc 字节缓冲输出流,测试加缓冲流拷贝文件时间
*/
public class BufferedOutputStreamDemo1 {
public static void main(String[] args) {
File source = new File("D:\\系统镜像\\ubuntu-18.04.4-live-server-amd64.iso");
File target = new File("F:\\ubuntu-18.04.4-live-server-amd64.iso");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
long start = System.currentTimeMillis();
bis = new BufferedInputStream(new FileInputStream(source));
bos = new BufferedOutputStream(new FileOutputStream(target));
int len = 0;
byte[] buffer = new byte[1024];
while((len = bis.read(buffer)) != -1) {
bos.write(buffer,0,len);
}
long end = System.currentTimeMillis();
System.out.println("拷贝完成,一共耗时" + (end-start)/60 + "s" );
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bis.close();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果
测试结果表明使用缓冲流读写文件时间会缩短3分之一,强烈建议使用缓冲流
BufferedReader和BufferedWriter字符缓冲流
字符缓冲流独有的2个方法
BufferedReader:public String readLine():读一行数据。 读取到最后返回null。
BufferedWriter:public void newLine():换行,该方法内部调用了lineSeparator,它表示的换行符。
package com.mike.io.eg;
import java.io.IOException;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.File;
/**
* @author mike
* @date 2020-11-27
* @desc 字符输入缓冲流
*/
public class BufferedReaderDemo {
public static void main(String[] args) {
//定义缓冲流对象
BufferedReader br = null;
//定义文件对象
File file = new File("F://foo.txt");
try {
br = new BufferedReader(new FileReader(file));
String data;
while((data = br.readLine()) != null) {
System.out.println(data);
}
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.mike.io.eg;
import java.io.IOException;
import java.io.File;
import java.io.FileWriter;
import java.io.BufferedWriter;
/**
* @author mike
* @date 2020-11-27
* @desc 字符输出缓冲流
*/
public class BufferedWriterDemo {
public static void main(String[] args) {
//定义缓冲流
BufferedWriter bw = null;
//定义文件对象
File file = new File("F:\\foo.txt");
try {
bw = new BufferedWriter(new FileWriter(file));
bw.write("我的名字叫mike");
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}