您好,登錄后才能下訂單哦!
import java.io.BufferedOutputStream;? ?
import java.io.File;? ?
import java.io.FileOutputStream;? ?
import java.io.FileWriter;? ?
import java.nio.ByteBuffer;? ?
import java.nio.channels.FileChannel;? ?
??
/**??
?* 測試各種寫文件的方法性能。??
?*? ?
?* @author 老紫竹 JAVA世紀(jì)網(wǎng)(java2000.net)??
?*? ?
?*/??
public class T {? ?
??
? public static void main(String[] args) {? ?
? ? FileOutputStream out = null;? ?
? ? FileOutputStream outSTr = null;? ?
? ? BufferedOutputStream Buff = null;? ?
? ? FileWriter fw = null;? ?
? ? int count = 1000000;// 寫文件行數(shù)? ?
? ? try {? ?
? ? ? byte[] bs = "測試java 文件操作/r/n".getBytes();? ?
? ? ? out = new FileOutputStream(new File("C:/add.txt"));? ?
? ? ? long begin = System.currentTimeMillis();? ?
? ? ? for (int i = 0; i < count; i++) {? ?
? ? ? ? out.write(bs);? ?
? ? ? }? ?
? ? ? out.close();? ?
? ? ? long end = System.currentTimeMillis();? ?
? ? ? System.out.println("FileOutputStream執(zhí)行耗時:" + (end - begin) + " 豪秒");? ?
??
? ? ? outSTr = new FileOutputStream(new File("C:/add0.txt"));? ?
? ? ? Buff = new BufferedOutputStream(outSTr);? ?
? ? ? long begin0 = System.currentTimeMillis();? ?
? ? ? for (int i = 0; i < count; i++) {? ?
? ? ? ? Buff.write(bs);? ?
? ? ? }? ?
? ? ? Buff.flush();? ?
? ? ? Buff.close();? ?
? ? ? long end0 = System.currentTimeMillis();? ?
? ? ? System.out.println("BufferedOutputStream執(zhí)行耗時:" + (end0 - begin0) + " 豪秒");? ?
??
? ? ? fw = new FileWriter("C:/add2.txt");? ?
? ? ? long begin3 = System.currentTimeMillis();? ?
? ? ? for (int i = 0; i < count; i++) {? ?
? ? ? ? fw.write("測試java 文件操作/r/n");? ?
? ? ? }? ?
? ? ? fw.flush();? ?
? ? ? fw.close();? ?
? ? ? long end3 = System.currentTimeMillis();? ?
? ? ? System.out.println("FileWriter執(zhí)行耗時:" + (end3 - begin3) + " 豪秒");? ?
??
? ? ? long begin4 = System.currentTimeMillis();? ?
? ? ? String path = "C:/add3.txt";? ?
? ? ? ByteBuffer bb = ByteBuffer.wrap(bs);? ?
? ? ? FileChannel out2 = new FileOutputStream(path).getChannel();? ?
? ? ? for (int i = 0; i < count; i++) {? ?
? ? ? ? out2.write(bb);? ?
? ? ? ? bb.rewind();? ?
? ? ? }? ?
??
? ? ? out2.close();? ?
? ? ? long end4 = System.currentTimeMillis();? ?
? ? ? System.out.println("FileChannel執(zhí)行耗時:" + (end4 - begin4) + " 豪秒");? ?
? ? } catch (Exception e) {? ?
? ? ? e.printStackTrace();? ?
? ? } finally {? ?
? ? ? try {? ?
? ? ? ? fw.close();? ?
? ? ? ? Buff.close();? ?
? ? ? ? outSTr.close();? ?
? ? ? ? out.close();? ?
? ? ? } catch (Exception e) {? ?
? ? ? ? e.printStackTrace();? ?
? ? ? }? ?
? ? }? ?
? }? ?
}??
?
在我的筆記本上,運行結(jié)果如下
FileOutputStream執(zhí)行耗時:4891 豪秒
BufferedOutputStream執(zhí)行耗時:78 豪秒
FileWriter執(zhí)行耗時:438 豪秒
FileChannel執(zhí)行耗時:2812 豪秒
當(dāng)然最終生成的文件都是正確的
總結(jié):
BufferedOutputStream 由于有緩沖,性能明顯好
FileOutputStream 性能最差,因為其每次都寫入字節(jié)。
FileWriter 性能很一般
FileChannel使用了nio,但如果沒有緩沖,必能太指望性能了
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。