写入方法:

  

/**
*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写文件效率的更多相关文章

  1. Java IO 写文件

    package com.lf.fileproject; import java.io.File; import java.io.FileOutputStream; import org.junit.T ...

  2. [测试]java IO写入文件效率——几种方法比较

    各类写入方法 /** *1 按字节写入 FileOutputStream * * @param count 写入循环次数 * @param str 写入字符串 */ public void outpu ...

  3. Java IO :文件

    在java应用程序中,文件是一种常用的数据源或者存储数据的媒介.所以这一小节将会对Java中文件的使用做一个简短的概述.这里只提供一些必要的知识点. 通过Java IO读文件 如果你需要在不同端之间读 ...

  4. java io读写文件

    java io读写文件相关阅读:http://www.cnblogs.com/wing011203/archive/2013/05/03/3056535.html public class DemoI ...

  5. java(IO)读写文件乱码转换UTF-8问题

    java(IO)读写文件乱码转换UTF-8问题 读取文件 String Content = ""; // 文件很长的话建议使用StringBuffer try { FileInpu ...

  6. Java IO 流-- 文件拷贝

    IO流操作套路: 1.创建源: 2.选择流: 3.操作: 4.释放资源 上代码: package com.xzlf.io; import java.io.File; import java.io.Fi ...

  7. java中IO写文件工具类

    以下是一些依据经常使用java类进行组装的对文件进行操作的类,平时,我更喜欢使用Jodd.io中提供的一些对文件的操作类,里面的方法写的简单易懂. 当中jodd中提供的JavaUtil类中提供的方法足 ...

  8. Java不写文件,LOAD DATA LOCAL INFILE大批量导入数据到MySQL的实现(转)

    MySQL使用load data local infile 从文件中导入数据比insert语句要快,MySQL文档上说要快20倍左右.但是这个方法有个缺点,就是导入数据之前,必须要有文件,也就是说从文 ...

  9. java读/写文件

    读取文件参考:https://blog.csdn.net/weixin_42129373/article/details/82154471 写入文件参考:https://blog.csdn.net/B ...

随机推荐

  1. 暴风雨中的 online :.net core 版博客站点遭遇的高并发问题进展

    今天暴风雨袭击了杭州,而昨天暴风雨(高并发问题)席卷了园子,留下一片狼藉. 在前天傍晚,我们进行了 .net core 版博客站点的第二次发布尝试,在发布后通过 kestrel 直接监听取代 ngin ...

  2. 统一流控服务开源:基于.Net Core的流控服务

    先前有一篇博文,梳理了流控服务的场景.业界做法和常用算法 统一流控服务开源-1:场景&业界做法&算法篇 最近完成了流控服务的开发,并在生产系统进行了大半年的验证,稳定可靠.今天整理一下 ...

  3. Java学习|多线程学习笔记

    什么是线程?     可以理解为进程中独立运行的字任务.   使用多线程:     1.继承Thread类:从源码可以看到,Thread累实现了Runnable接口.         如果多次调用st ...

  4. c语言实现基本的数据结构(六) 串

    #include <stdio.h> #include <tchar.h> #include <stdlib.h> // TODO: 在此处引用程序需要的其他头文件 ...

  5. Python|队列Queue

    一 前言 本文算是一次队列的学习笔记,Queue 模块实现了三种类型的队列,它们的区别仅仅是队列中元素被取回的顺序.在 FIFO 队列中,先添加的任务先取回.在 LIFO 队列中,最近被添加的元素先取 ...

  6. 打造适用于c#的feign

    之前因为工作原因使用spring cloud全家桶开发过若干项目,发现其中的feign非常好用,以前开发接口客户端的时候都是重复使用HttpClient实现业务,每次新增接口都十分繁琐,故萌生了自定义 ...

  7. springBoot与Swagger2的整合

    1.在项目pom文件中引入swagger2的jar包 <!-- swagger2开始 --> <dependency> <groupId>io.springfox& ...

  8. 实战SpringCloud响应式微服务系列教程(第四章)

    接上一篇: 实战SpringCloud响应式微服务系列教程(第一章) 实战SpringCloud响应式微服务系列教程(第二章) 实战SpringCloud响应式微服务系列教程(第三章) 1.1.4 引 ...

  9. 如何运用PHP+REDIS解决负载均衡后的session共享问题

    一.为什么要使用Session共享? 稍大一些的网站,通常都会有好几个服务器,每个服务器运行着不同功能的模块,使用不同的二级域名,而一个整体性强的网站,用户系统是统一的,即一套用户名.密码在整个网站的 ...

  10. 持续集成高级篇之Jekins参数传入与常见任务

    系列目录 有的童鞋可能已经发现,PipeLine项目与自由式项目相比,可配置的项少了很多,比如说环境变量定义,所有步骤完成后执行动作,拉git代码库等.其实这些功能并没有缺,而是配置的方式不一样了,以 ...