import java.io.File;java
import java.io.FileOutputStream;ide
import java.io.*;測試
public class FileTest {get
public static void main(String[] args) {it
FileOutputStream out = null;io
FileOutputStream outSTr = null;class
BufferedOutputStream Buff = null;import
FileWriter fw = null;im
int count = 1000;//寫文件行數d3
try {
//通過測試:FileOutputStream執行耗時: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執行耗時:" + (end - begin) + " 毫秒");
//通過測試:ufferedOutputStream執行耗時: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執行耗時:" + (end0 - begin0) + " 毫秒");
//通過測試:FileWriter執行耗時: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執行耗時:" + (end3 - begin3) + " 毫秒");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fw.close();
Buff.close();
outSTr.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}