import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* 测试各种写文件的方法性能。
*
* @author 老紫竹 JAVA世纪网(java2000.net)
*
*/
public class T {
public static void main(String[] args) {
FileOutputStream out = null;
FileOutputStream outSTr = null;
BufferedOutputStream Buff = null;
FileWriter fw = null;
int count = 1000000;// 写文件行数
try {
byte[] bs = "测试java 文件操作\r\n".getBytes();
out = new FileOutputStream(new File("C:/add.txt"));
long begin = System.currentTimeMillis();
for (int i = 0; i < count; i++) {
out.write(bs);
}
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(bs);
}
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.flush();
fw.close();
long end3 = System.currentTimeMillis();
System.out.println("FileWriter执行耗时:" + (end3 - begin3) + " 豪秒");
long begin4 = System.currentTimeMillis();
String path = "C:/add3.txt";
ByteBuffer bb = ByteBuffer.wrap(bs);
FileChannel out2 = new FileOutputStream(path).getChannel();
for (int i = 0; i < count; i++) {
out2.write(bb);
bb.rewind();
}
out2.close();
long end4 = System.currentTimeMillis();
System.out.println("FileChannel执行耗时:" + (end4 - begin4) + " 豪秒");
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fw.close();
Buff.close();
outSTr.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
转载自:http://www.java2000.net/p12986
在我的笔记本上,运行结果如下
FileOutputStream执行耗时:4891 豪秒
BufferedOutputStream执行耗时:78 豪秒
FileWriter执行耗时:438 豪秒
FileChannel执行耗时:2812 豪秒
当然最终生成的文件都是正确的
总结:
BufferedOutputStream 由于有缓冲,性能明显好
FileOutputStream 性能最差,因为其每次都写入字节。
FileWriter 性能很一般
FileChannel使用了nio,但如果没有缓冲,必能太指望性能了
分享到:
相关推荐
Java 中对文件的读写操作之比较
java读写文件的方法.pdf
java读写文件的方法,java操作文件
介绍了如何通过java来读写xml的方法,其中介绍了几种方法,如:DOM、SAX、JDOM、JAXB、dom4j.
主要实现的是登录服务器操作服务器的中的文件数据,支持读写的操作。主要使用的方法getProperties是设置配置的login(参数一是访问服务器的配置,参数二是设置读还是写)方法是读写连接服务器
代码详细描述了通过java读写xml文件的方法,值得一看
netcdf即nc文件的读写实例,java版本的,不懂的地方可以问我,lishirongtech@126.com
java 删除某行和读写txt文件 java中貌似没有直接删除某行的方法,这里用的方法是把文件里的内容读出来,写入一个String里,(不要的那行找到,不要写入String),然后再重新写入文件
C++,java读写二进制文件方法介绍.docx
主要给大家介绍了关于Java文件、文件夹权限修改的两种方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Java具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
使用Java写入大数据文件的方法,分文件写入。
JAVA本身并没有读写INI文件的现成方法,有些人自己编写方法来读写INI文件,但是这样的方法或多或少的存在着一些问题。此示例旨在利用本地的WIN32API函数来读写INI文件,这样可以保证INI读写的正确性。 通过此例子...
– 输入流:只能从中读取字节数据,而不能向其写出数据 – 输出流:只能向其写入字节数据,而不能从中读取数据 • 按照流所处理的数据类型 – 字节流:用于处理字节数据。 – 字符流:用于处理Unicode字符数据。 • ...
java读取本地.db文件简单方法,sqlite数据库类型文件获取数据
一种基于Java注解和反射机制的Excel文件读写操作方法.pdf
经过了我的测试,可以直接用的 例如: public void ... sb.append("这是第"+i+"行:前面介绍的各种方法都不关用,为什么总是奇怪的问题 "); out.write(sb.toString().getBytes("utf-8")); } out.close(); }
C++,java读写二进制文件方法介绍.pdf java从第九页开始
一个简单的提供覆盖式和追加式写文件功能的JAVA类,可调用其覆盖式方法创建新文件或重置老文件内容,调用其追加式方法在已有文件中增加新内容。
java读取属性文件通用方法,以及日志记录