一、关于Java流速度问题。
1. 排名(快 -------> 慢):
-
缓冲流(BufferedReader/BufferedWriter)
-
混合使用转换流和缓冲流
-
转换流(InputStreamReader/OutputStreamWriter)
2. 测试代码如下:
- 下面代码中字节输入流(FileInputStream)和字节输出流(FileOutpuStream)没做处理。
import java.io.*;
public class TestIOMain {
public static void main(String[] args) {
String oldFile = "C:\\Users\\Yu\\Desktop\\sunshine.txt";
String newFile = "C:\\Users\\Yu\\Desktop\\";
bufferedIOTest(oldFile,newFile+"buffreed.txt");
changeIOTest(oldFile,newFile+"changeIO.txt");
mixUseIO(oldFile,newFile+"mixUserIO.txt");
}
public static void mixUseIO(String oldFile,String newFile){
// 一起使用
long s = System.currentTimeMillis();
// 转换流
InputStreamReader isr = null;
OutputStreamWriter osw = null;
// 缓冲流
BufferedReader br = null;
BufferedWriter bw = null;
char[] chars = new char[1024];
int len;
try {
// 输入流
isr = new InputStreamReader(new FileInputStream(oldFile),"gbk");
br = new BufferedReader(isr);
// 输出流
osw = new OutputStreamWriter(new FileOutputStream(newFile),"gbk");
bw = new BufferedWriter(osw);
while ((len = br.read(chars))!=-1){
bw.write(chars,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw!=null){
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isr!=null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("mixUseIO:"+(System.currentTimeMillis()-s));
}
public static void changeIOTest(String oldFile,String newFile){
long s = System.currentTimeMillis();
// 转换流
InputStreamReader isr = null;
OutputStreamWriter osw = null;
char[] chars = new char[1024];
int len;
try {
isr = new InputStreamReader(new FileInputStream(oldFile), "gbk");
osw = new OutputStreamWriter(new FileOutputStream(newFile),"gbk");
while ((len = isr.read(chars))!=-1){
osw.write(chars,0,len);
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}finally {
if (isr!=null){
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (osw!=null){
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("changeIOTest:"+(System.currentTimeMillis()-s));
}
public static void bufferedIOTest(String oldFile,String newFile){
long s = System.currentTimeMillis();
// 缓冲流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
byte[] bytes = new byte[1024];
int len;
try {
bis = new BufferedInputStream(new FileInputStream(oldFile));
bos = new BufferedOutputStream(new FileOutputStream(newFile));
while ((len=bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("bufferedIOTest:"+(System.currentTimeMillis()-s));
}
}