java位元組流和字元流的比較哦啊

為它奮鬥的發表於2015-05-17
package base;


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;


public class Test {
//比較基本位元組流和緩衝流在複製較大檔案時的效率,檔案過G才明顯
public static void t1() throws Exception {
long begin = System.currentTimeMillis();
// 1.架兩個管道
File f = new File("d:\\json-lib-2.4-jdk15-sources.jar");
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream("d:\\java\\json-lib-2.4-jdk15-sources.jar");
// 2.來輛車
byte[] temp = new byte[1 * 1024]; // 1K
// 3.反覆裝車卸車(邊讀邊寫)
while (true) {
// 裝車
int res = fis.read(temp);//從輸入流中讀到temp中
if (res == -1)
break;
// 卸車
fos.write(temp, 0, res);//從temp中寫到輸出流中去
}
// 4.關閉兩個流
fis.close();
fos.close();
long end = System.currentTimeMillis();
System.out.println(end - begin);
}


public static void t2() throws Exception {//字元處理的快一些
long begin = System.currentTimeMillis();
// 1.架兩個管道
File f = new File("d:\\json-lib-2.4-jdk15-sources.jar");
BufferedInputStream fis = new BufferedInputStream(
new FileInputStream(f));
BufferedOutputStream fos = new BufferedOutputStream(
new FileOutputStream("d:\\java\\json-lib-2.4-jdk15-sources.jar"));
// 2.來輛車
byte[] temp = new byte[1 * 1024]; // 1K
// 3.反覆裝車卸車(邊讀邊寫)
while (true) {
// 裝車
int res = fis.read(temp);
if (res == -1)
break;
// 卸車
fos.write(temp, 0, res);
}


// 4.關閉兩個流
fis.close();
fos.close();
long end = System.currentTimeMillis();
System.out.println(end - begin);
}


public static void main(String[] args) throws Exception {
//t1();
t2();
}
}

相關文章