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几乎没区别,时间有些微的差别,但内部机制是一样的。实际上用的6.
Java IO写文件效率的更多相关文章
- Java IO 写文件
package com.lf.fileproject; import java.io.File; import java.io.FileOutputStream; import org.junit.T ...
- [测试]java IO写入文件效率——几种方法比较
各类写入方法 /** *1 按字节写入 FileOutputStream * * @param count 写入循环次数 * @param str 写入字符串 */ public void outpu ...
- Java IO :文件
在java应用程序中,文件是一种常用的数据源或者存储数据的媒介.所以这一小节将会对Java中文件的使用做一个简短的概述.这里只提供一些必要的知识点. 通过Java IO读文件 如果你需要在不同端之间读 ...
- java io读写文件
java io读写文件相关阅读:http://www.cnblogs.com/wing011203/archive/2013/05/03/3056535.html public class DemoI ...
- java(IO)读写文件乱码转换UTF-8问题
java(IO)读写文件乱码转换UTF-8问题 读取文件 String Content = ""; // 文件很长的话建议使用StringBuffer try { FileInpu ...
- Java IO 流-- 文件拷贝
IO流操作套路: 1.创建源: 2.选择流: 3.操作: 4.释放资源 上代码: package com.xzlf.io; import java.io.File; import java.io.Fi ...
- java中IO写文件工具类
以下是一些依据经常使用java类进行组装的对文件进行操作的类,平时,我更喜欢使用Jodd.io中提供的一些对文件的操作类,里面的方法写的简单易懂. 当中jodd中提供的JavaUtil类中提供的方法足 ...
- Java不写文件,LOAD DATA LOCAL INFILE大批量导入数据到MySQL的实现(转)
MySQL使用load data local infile 从文件中导入数据比insert语句要快,MySQL文档上说要快20倍左右.但是这个方法有个缺点,就是导入数据之前,必须要有文件,也就是说从文 ...
- java读/写文件
读取文件参考:https://blog.csdn.net/weixin_42129373/article/details/82154471 写入文件参考:https://blog.csdn.net/B ...
随机推荐
- shell 提取文件名和目录名
转自http://blog.csdn.net/universe_hao/article/details/52640321 shell 提取文件名和目录名 在写shell脚本中,经常会有需要对路径和文件 ...
- python+unittest框架第四天unittest之断言(一)
unittest中的测试断言分两天总结,hhh其实内容不多,就是懒~ 断言的作用是什么? 答:设置测试断言以后,能帮助我们判断测试用例执行结果. 我们先看下unittest支持的断言有哪些: 对上面 ...
- 小白学Python(6)——python-pptx 添加图表
添加图表 以下代码在新演示文稿中添加单系列柱形图 from pptx import Presentation from pptx.chart.data import CategoryChartData ...
- 01 Python网络爬虫简介
什么是爬虫 爬虫就是通过编写程序模拟浏览器上网,然后去互联网上爬取/获取数据的过程. 爬虫的分类 - 通用爬虫:就是爬取互联网中的一整张页面内容. - 聚焦爬虫:根据指定的需求爬取页面中指定的局部内容 ...
- 多线程之pthread, NSThread, NSOperation, GCD
关于多线程会有一系列如下:多线程之概念解析 多线程之pthread, NSThread, NSOperation, GCD 多线程之NSThread 多线程之NSOperation 多线程之GCD p ...
- linux安装杀软 clamAV
ClamAV 是Linux平台最受欢迎的杀毒软件,ClamAV 属于免费的开源软件,支持多种平台.ClamAV是基于病毒扫描的命令行工具,但同时也有支持图形界面的ClamTK工具.ClamAV 主要用 ...
- SpringMVC源码分析4:DispatcherServlet如何找到正确的Controller
SpringMVC是目前主流的Web MVC框架之一. 我们使用浏览器通过地址 http://ip:port/contextPath/path进行访问,SpringMVC是如何得知用户到底是访问哪个 ...
- 【乘风破浪】Android系统启动流程整理
前言 对于一个Android应用层开发者来说,了解Android系统的启动流程对理解Android系统有很大的帮助.这其中包含了大量的细节,而且前面很多步骤包含了C/C++实现的native层逻辑,作 ...
- Delphi - 创建text文件并添加数据到文件中
创建文本文件 代码如下: //创建一个文本文件 procedure CreateTextFile(); var AssignFile(TF,'C:\tmp\1.txt'); ReWrite(TF); ...
- D-Distance_2019牛客暑期多校训练营(第八场)
题目链接 Distance 题意 1<=nmh,q<=1e5 q个操作 1 x y z往坐标里加入一个点 2 x y z查询距离该点最近的点的距离(曼哈顿距离) 题解 做法一 将要插入的点 ...