File Test--------- 之性能优化详解
import java.io.File;
import java.io.FileOutputStream;
import java.io.*;
?
public class FileTest {
?
?? ?public FileTest() {
?
?? ?}
?
?? ?public static void main(String[] args) {
?
?? ? ? ?FileOutputStream out = null;
?
?? ? ? ?FileOutputStream outSTr = null;
?
?? ? ? ?BufferedOutputStream Buff=null;
?
?? ? ? ?FileWriter fw = null;
?
?? ? ? ?int count=1000;//写文件行数
?
?? ? ? ?try {
?
?? ? ? ? ? ?out = new FileOutputStream(new File("C:/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) + " 豪秒");
?
?
?
?? ? ? ? ? ?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("测试java 文件操作\r\n".getBytes());
?
?? ? ? ? ? ?}
?
?? ? ? ? ? ?Buff.flush();
?
?? ? ? ? ? ?Buff.close();
?
?? ? ? ? ? ?long end0 = System.currentTimeMillis();
?
?? ? ? ? ? ?System.out.println("BufferedOutputStream执行耗时:" + (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.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();
?
?? ? ? ? ? ?}
?
?? ? ? ?}
?
?? ?}
?
} ? ?
?
?
?
以下结果经过多次执行,取常出现的数据,由于只是简单比较,不做具体统计。
?
1.当count=1000的,即写文件1000行的时候,写出的文件大小为18.5KB:
FileOutputStream执行耗时:46 豪秒
BufferedOutputStream执行耗时:31 豪秒
FileWriter执行耗时:15 豪秒
?
2.当count=10000的,即写文件10000行的时候,写出的文件大小为185KB:
FileOutputStream执行耗时:188 豪秒
BufferedOutputStream执行耗时:32 豪秒
FileWriter执行耗时:16 豪秒
?
3.当count=100000的,即写文件100000行的时候,写出的文件大小为1856KB:
FileOutputStream执行耗时:1266 豪秒
BufferedOutputStream执行耗时:125 豪秒
FileWriter执行耗时:93 豪秒
?
4.当count=1000000的,即写文件1000000行的时候,写出的文件大小为18555KB:
FileOutputStream执行耗时:12063 豪秒
BufferedOutputStream执行耗时:1484 豪秒
FileWriter执行耗时:969 豪秒
?
?? 由以上数据可以看到,假如不用缓冲流BufferedOutputStream,FileOutputStream写文件的鲁棒性是很不好的。当写 1000000行的文件的时候,FileOutputStream比FileWriter要慢11094毫秒(11秒), BufferedOutputStream比FileWriter慢515毫秒。
?? 不要小看这几秒的时间。当操作的数据量很大的时候,这点性能的差距就会很大了。在通用数据迁移工具导出数据库2千万条记录生成sql脚本文件的时候,性能相差10分钟以上。
1 楼 ray_linn 2011-03-01 请翻翻书,查看一下啥叫鲁棒性? 你的Fileoutputstream崩溃吗? 2 楼 lgs0626 2011-03-02 ray_linn 写道请翻翻书,查看一下啥叫鲁棒性? 你的Fileoutputstream崩溃吗?
鲁棒性就是系统的健壮性。它是在异常和危险情况下系统生存的关键。比如说,计算机软件在输入错误、磁盘故障、网络过载或有意攻击情况下,能否不死机、不崩溃,就是该软件的鲁棒性。所谓“鲁棒性”,是指控制系统在一定(结构,大小)的参数摄动下,维持某些性能的特性。根据对性能的不同定义,可分为稳定鲁棒性和性能鲁棒性。以闭环系统的鲁棒性作为目标设计得到的固定控制器称为鲁棒控制器。