java写文件性能比较

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();
			}
		}
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值