您好,登录后才能下订单哦!
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;//写文件行数
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();
}
}
}
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。