[测试]java IO写入文件效率——几种方法比较
各类写入方法
/**
*1 按字节写入 FileOutputStream
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void outputStreamTest(int count, String str) {
File f = new File("f:test1.txt");
OutputStream os = null;
try {
os = new FileOutputStream(f);
for (int i = 0; i < count; i++) {
os.write(str.getBytes());
}
os.flush();
System.out.println("file's long:" + f.length());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
*2 按字节缓冲写入 BufferedOutputStream
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedOutputTest(int count, String str) {
File f = new File("f:test2.txt");
BufferedOutputStream bos = null;
try {
OutputStream os = new FileOutputStream(f);
bos = new BufferedOutputStream(os);
for (int i = 0; i < count; i++) {
bos.write(str.getBytes());
}
bos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
*3 按字符写入 FileWriter
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void fileWriteTest(int count, String str) {
File f = new File("f:test.txt");
Writer writer = null;
try {
writer = new FileWriter(f);
for (int i = 0; i < count; i++) {
writer.write(str);
}
writer.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} /**
*4 按字符缓冲写入 BufferedWriter
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedWriteTest(int count, String str) {
File f = new File("f:test3.txt");
OutputStreamWriter writer = null;
BufferedWriter bw = null;
try {
OutputStream os = new FileOutputStream(f);
writer = new OutputStreamWriter(os);
bw = new BufferedWriter(writer);
for (int i = 0; i < count; i++) {
bw.write(str);
}
bw.flush();
if(f.exists()){
f.delete();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
*5 按字符缓冲写入 BufferedWriter and BufferedOutputStream
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedWriteAndBufferedOutputStreamTest(int count, String str) {
File f = new File("f:test4.txt");
BufferedOutputStream bos=null;
OutputStreamWriter writer = null;
BufferedWriter bw = null;
try {
OutputStream os = new FileOutputStream(f);
bos=new BufferedOutputStream(os);
writer = new OutputStreamWriter(bos);
bw = new BufferedWriter(writer);
for (int i = 0; i < count; i++) {
bw.write(str);
}
bw.flush();
if(f.exists()){
f.delete();
System.out.println("delete---");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} /**
*6 按字符缓冲写入 BufferedWriter and FileWriter
*
* @param count 写入循环次数
* @param str 写入字符串
*/
public void bufferedWriteAndFileWriterTest(int count, String str) {
File f = new File("f:test5.txt");
FileWriter fw=null;
BufferedWriter bw = null;
try {
fw=new FileWriter(f);
bw = new BufferedWriter(fw);
for (int i = 0; i < count; i++) {
bw.write(str);
}
bw.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
bw.close();
if(f.exists()){
f.delete();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
测试写入类
public static void main(String[] args) {
String str = "abcdefghiJKLMN!";
int count = 1000000;
TestOutputStream t = new TestOutputStream(); //1.fileWrite's time
long start = System.currentTimeMillis();
t.fileWriteTest(count, str);
long end = System.currentTimeMillis();
System.out.println("fileWrite's time---------" + (start - end)); //2.outputStreamTest's time
start = System.currentTimeMillis();
t.outputStreamTest(count, str);
end = System.currentTimeMillis();
System.out.println("outputStreamTest's time---------" + (start - end)); //3.bufferedOutputTest's time
start = System.currentTimeMillis();
t.bufferedOutputTest(count, str);
end = System.currentTimeMillis();
System.out.println("bufferedOutputTest's time---------" + (start - end)); //4.bufferedWriteTest's time
start = System.currentTimeMillis();
t.bufferedWriteTest(count, str);
end = System.currentTimeMillis();
System.out.println("bufferedWriteTest's time---------" + (start - end)); //5.bufferedWrite And FileWriterTest's time
start = System.currentTimeMillis();
t.bufferedWriteAndFileWriterTest(count, str);
end = System.currentTimeMillis();
System.out.println("bufferedWrite And FileWriterTest's time---------" + (start - end)); //6.bufferedWrite And BufferedOutputStreamTest's time
start = System.currentTimeMillis();
t.bufferedWriteAndBufferedOutputStreamTest(count, str);
end = System.currentTimeMillis();
System.out.println("bufferedWrite And BufferedOutputStreamTest's time---------" + (start - end));
}
测试结果
/**
* 测试结果
*
* 1.file's long:16kb
*
fileWrite's time----------36
outputStreamTest's time----------167
bufferedOutputTest's time----------17
bufferedWriteTest's time----------14
bufferedWrite And FileWriterTest's time----------9
bufferedWrite And BufferedOutputStreamTest's time----------12
*
* 2.file's long:1600kb
*
fileWrite's time----------69
outputStreamTest's time----------1282
bufferedOutputTest's time----------68
bufferedWriteTest's time----------40
bufferedWrite And FileWriterTest's time----------52
bufferedWrite And BufferedOutputStreamTest's time----------37
*
* 3.file's long:16000kb
*
fileWrite's time----------555
outputStreamTest's time----------12448
bufferedOutputTest's time----------599
bufferedWriteTest's time----------346
bufferedWrite And FileWriterTest's time----------316
bufferedWrite And BufferedOutputStreamTest's time----------358
*
*4.file's long:160000kb
*
fileWrite's time----------5203
outputStreamTest's time----------127182
bufferedOutputTest's time----------5972
bufferedWriteTest's time----------3445 最优
bufferedWrite And FileWriterTest's time----------5904
bufferedWrite And BufferedOutputStreamTest's time----------5353 *
*5.file's long:1600000kb
*
fileWrite's time----------50416
outputStreamTest's time----------1303242
bufferedOutputTest's time----------60931
bufferedWriteTest's time----------46697
bufferedWrite And FileWriterTest's time----------48710
bufferedWrite And BufferedOutputStreamTest's time----------64354
*/
总结:
如果按字符和字节来分类,除方法1和2,其余都是按字符写入文件,字符写入一般比字节快;看java API可知,FileWriter的父类就是OutputStreamWriter,他俩都是实现Writer类,从这点上来说,方法4和6几乎没区别,时间有些微的差别,但内部机制是一样的。
[测试]java IO写入文件效率——几种方法比较的更多相关文章
- java分享第十六天( java读取properties文件的几种方法&java配置文件持久化:static块的作用)
java读取properties文件的几种方法一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取Propert ...
- java写入文件的几种方法分享
转自:http://www.jb51.net/article/47062.htm 一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所 ...
- java写入文件的几种方法小结
一,FileWritter写入文件 FileWritter, 字符流写入字符到文件.默认情况下,它会使用新的内容取代所有现有的内容,然而,当指定一个true (布尔)值作为FileWritter构造函 ...
- Java IO写文件效率
写入方法: /** *1 按字节写入 FileOutputStream * * @param count 写入循环次数 * @param str 写入字符串 */ public void output ...
- 精----Java读取xml文件的四种方法
xml文件: Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VA ...
- Java读取Excel文件的几种方法
Java读取 Excel 文件的常用开源免费方法有以下几种: 1. JDBC-ODBC Excel Driver 2. jxl.jar 3. jcom.jar 4. poi.jar 简单介绍: 百度文 ...
- java读取properties文件的几种方法
一.项目中经常会需要读取配置文件(properties文件),因此读取方法总结如下: 1.通过java.util.Properties读取 Properties p=new Properties(); ...
- java读取xml文件的四种方法
Xml代码 <?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> ...
- iOS开发小技巧--边接受数据边写入文件的两种方法
一.NSFileHanle 使用注意点:在往文件写入数据时,必须创建一个空的文件 指定文件写入的方式 -- 覆盖还是追加 最后记得关闭 <1>代码是在大文件传输的练习中截取的.写入数据之前 ...
随机推荐
- HDU 1242 Rescue(优先队列)
题目来源: http://acm.hdu.edu.cn/showproblem.php?pid=1242 题目描述: Problem Description Angel was caught by ...
- ThinkPHP3.2基础知识(三)
1.如何开启调试模式,开启调试模式有什么用处? // 开启调试模式 建议开发阶段开启 部署阶段注释或者设为false define('APP_DEBUG',True); 开启调试模式的用处:方便及时发 ...
- Harris角点检测原理分析
看到一篇从数学意义上讲解Harris角点检测很透彻的文章,转载自:http://blog.csdn.net/newthinker_wei/article/details/45603583 主要参考了: ...
- (Release Candidate)Candidate
RC:(Release Candidate)Candidate是候选人的意思,用在软件或者操作系统上就是候选版本
- Android 问题
1.Cannot refer to a non-final variable 解决方法 内部类如果要引用外部类的变量,则该变量必须为final,这是规定 2.error: No resource id ...
- jsonArray与 jsonObject区别与js取值
一.JSONObject和JSONArray的数据表示形式 JSONObject的数据是用 { } 来表示的, 例如: { "id" : "123", & ...
- python3 第七章 - 循环语句
为了让计算机能计算成千上万次的重复运算,我们就需要循环语句. Python中的循环语句有 while for 循环语句的执行过程,如下图: while 循环 Python中while语句的一般形式: ...
- js 数组与对象的区别
学习javascript的时候,我曾经一度搞不清楚”数组”(array)和”对象”(object)的根本区别在哪里,两者都可以用来表示数据的集合. 比如有一个数组a=[1,2,3,4],还有一个对 ...
- aliyun 购买的linux安装tomcat
按照网上的教程,下载tomcat,解压(即安装),启动,发现无法访问.有说端口未开放,修改/etc/sysconfig/iptables,添加端口开放.未发现有此文件,只有iptables-confg ...
- maven多模块搭建
此时你会发现父模块含有如下内容 这是因为创建的maven项目都带有样例,比如上图的这张图片 各种artifact都是做什么的呢,@参考文章中给出了答案 怎么创建不带这些呢? 那就创建simple pr ...