您好,登錄后才能下訂單哦!
import java.io.File;
import java.io.FileOutputStream;
import java.io.*;
public class FileTest {
? ? public static void main(String[] args) {
? ? ? ? FileOutputStream out = null;
? ? ? ? FileOutputStream outSTr = null;
? ? ? ? BufferedOutputStream Buff = null;
? ? ? ? FileWriter fw = null;
? ? ? ? int count = 1000;//寫文件行數(shù)
? ? ? ? try {
? ? ? ? ? ? //經(jīng)過測試:FileOutputStream執(zhí)行耗時(shí):17,6,10 毫秒
? ? ? ? ? ? out = new FileOutputStream(new File("C:\\Users\\lee\\Desktop\\add.txt"));
? ? ? ? ? ? long begin = System.currentTimeMillis();
? ? ? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? ? ? out.write("測試java 文件操作\r\n".getBytes());
? ? ? ? ? ? }
? ? ? ? ? ? out.close();
? ? ? ? ? ? long end = System.currentTimeMillis();
? ? ? ? ? ? System.out.println("FileOutputStream執(zhí)行耗時(shí):" + (end - begin) + " 毫秒");
? ? ? ? ? ? //經(jīng)過測試:ufferedOutputStream執(zhí)行耗時(shí):1,1,1 毫秒
? ? ? ? ? ? outSTr = new FileOutputStream(new File("C:\\Users\\lee\\Desktop\\add0.txt"));
? ? ? ? ? ? Buff = new BufferedOutputStream(outSTr);
? ? ? ? ? ? long begin0 = System.currentTimeMillis();
? ? ? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? ? ? Buff.write("測試java 文件操作\r\n".getBytes());
? ? ? ? ? ? }
? ? ? ? ? ? Buff.flush();
? ? ? ? ? ? Buff.close();
? ? ? ? ? ? long end0 = System.currentTimeMillis();
? ? ? ? ? ? System.out.println("BufferedOutputStream執(zhí)行耗時(shí):" + (end0 - begin0) + " 毫秒");
? ? ? ? ? ? //經(jīng)過測試:FileWriter執(zhí)行耗時(shí):3,9,5 毫秒
? ? ? ? ? ? fw = new FileWriter("C:\\Users\\lee\\Desktop\\add2.txt");
? ? ? ? ? ? long begin3 = System.currentTimeMillis();
? ? ? ? ? ? for (int i = 0; i < count; i++) {
? ? ? ? ? ? ? ? fw.write("測試java 文件操作\r\n");
? ? ? ? ? ? }
? ? ? ? ? ? fw.close();
? ? ? ? ? ? long end3 = System.currentTimeMillis();
? ? ? ? ? ? System.out.println("FileWriter執(zhí)行耗時(shí):" + (end3 - begin3) + " 毫秒");
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? } finally {
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? fw.close();
? ? ? ? ? ? ? ? Buff.close();
? ? ? ? ? ? ? ? outSTr.close();
? ? ? ? ? ? ? ? out.close();
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? }
? ? ? ? }
? ? }
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。