java 向txt中写入字符串的几种方式效率测试代码
来源:互联网 发布:端口隔离 编辑:程序博客网 时间:2024/05/21 07:14
import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.PrintWriter;import java.io.Writer;public class Test3 {// 0.065秒 50000个"我是一颗自由小星星"写入到txt中,879KB public void printWriter(String str,String filepath,int count){try {PrintWriter pw=new PrintWriter(filepath);for(int i=0;i<count;i++){ pw.write(str); } pw.flush(); pw.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 0.127秒 50000个"我是一颗自由小星星"写入到txt中,879KB public void fileWrite(String str,String filepath,int count){ FileWriter writer; try { writer = new FileWriter(filepath,true); for(int i=0;i<count;i++){ writer.write(str); } writer.flush();//刷新内存,将内存中的数据立刻写出。 writer.close(); } catch (IOException e) { e.printStackTrace(); }}// 0.237秒 50000个"我是一颗自由小星星"写入到txt中,879KB public void fileOutputStream(String str,File file,int count){byte[] b = str.getBytes();try {FileOutputStream fos=new FileOutputStream(file); for(int i=0;i<count;i++){fos.write(b); } fos.flush(); fos.close();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();}catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}// 0.150秒 50000个"我是一颗自由小星星"写入到txt中,879KB public void bufferedWriter1(String str,File file,int count){FileWriter fw;try {fw = new FileWriter(file);BufferedWriter bw = new BufferedWriter (fw);for(int i=0;i<count;i++){fw.write(str);}fw.close(); fw.close(); } catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }// 0.172秒 50000个"我是一颗自由小星星"写入到txt中,879KB public void bufferedWriter2(String str,File file,int count){FileWriter fw;try {fw = new FileWriter(file);BufferedWriter bw = new BufferedWriter (fw);for(int i=0;i<count;i++){bw.write(str);}bw.flush();bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }public void bufferedWriter3(String str,File file,int count){PrintWriter fw;try {fw = new PrintWriter(file);BufferedWriter bw = new BufferedWriter (fw);for(int i=0;i<count;i++){bw.write(str);}bw.flush();bw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }public void bufferedWriter4(String str,File file,int count){PrintWriter fw;try {fw = new PrintWriter(file);BufferedWriter bw = new BufferedWriter (fw);for(int i=0;i<count;i++){fw.write(str);}fw.flush();fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }public void bufferedWriter5(String str,File file,int count){OutputStreamWriter fw;try {OutputStream outputstream =new FileOutputStream(file);fw = new OutputStreamWriter(outputstream);BufferedWriter bw = new BufferedWriter (fw);for(int i=0;i<count;i++){fw.write(str);}fw.flush();fw.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} }public static void main(String[] args) {Test3 test=new Test3();int count=20000000;int x=1;String filepath="D://1.txt";File file=new File(filepath);try {file.createNewFile();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}long time1 ;long time2 ;double time3;time1 = System.currentTimeMillis(); test.printWriter("我是一颗自由小星星", filepath,count);time2 =System.currentTimeMillis();time3=time2-time1;System.out.println("文件大小---"+file.length()/1024+"KB");System.out.println();System.out.println(x+"---printWriter time "+time3+"毫秒");//System.out.println("文件大小---"+file.length()/1024+"KB");x++;System.out.println(file.delete());time1 = System.currentTimeMillis(); test.fileWrite("我是一颗自由小星星", filepath,count);time2 =System.currentTimeMillis();time3=time2-time1;System.out.println(x+"---fileWriter time "+time3+"毫秒");//System.out.println("文件大小---"+file.length()/1024+"KB");x++;System.out.println(file.delete());time1 = System.currentTimeMillis(); test.fileOutputStream("我是一颗自由小星星", file,count);time2 =System.currentTimeMillis();time3=time2-time1;System.out.println(x+"---fileOutputStream time "+time3+"毫秒");//System.out.println("文件大小---"+file.length()/1024+"KB");x++;System.out.println(file.delete());time1 = System.currentTimeMillis(); test.bufferedWriter1("我是一颗自由小星星", file,count);time2 =System.currentTimeMillis();time3=time2-time1;System.out.println(x+"---bufferedWriter1_FileWriter time "+time3+"毫秒");//System.out.println("文件大小---"+file.length()/1024+"KB");x++;System.out.println(file.delete());time1 = System.currentTimeMillis(); test.bufferedWriter3("我是一颗自由小星星", file,count);time2 =System.currentTimeMillis();time3=time2-time1;System.out.println("文件大小---"+file.length()/1024+"KB");System.out.println(x+"---bufferedWriter3_printwriter time "+time3+"毫秒");System.out.println(file.delete());x++;time1 = System.currentTimeMillis(); test.bufferedWriter4("我是一颗自由小星星", file,count);time2 =System.currentTimeMillis();time3=time2-time1;//System.out.println("文件大小---"+file.length()/1024+"KB");System.out.println(x+"---bufferedWriter4_printwriter time "+time3+"毫秒");System.out.println(file.delete());x++;time1 = System.currentTimeMillis(); test.bufferedWriter2("我是一颗自由小星星", file,count);time2 =System.currentTimeMillis();time3=time2-time1;//System.out.println("文件大小---"+file.length()/1024+"KB");System.out.println(x+"---bufferedWriter2_filewriter time "+time3+"毫秒");System.out.println(file.delete());x++;time1 = System.currentTimeMillis(); test.bufferedWriter5("我是一颗自由小星星", file,count);time2 =System.currentTimeMillis();time3=time2-time1;System.out.println(x+"---bufferedWriter5_outputstreamwriter time "+time3+"毫秒");//System.out.println("文件大小---"+file.length()/1024+"KB");System.out.println(file.delete());} }
阅读全文
0 0
- java 向txt中写入字符串的几种方式效率测试代码
- java向txt中写入字符串的几种方式以及效率
- VC++向txt文件中写入字符串
- java读取写入文件几种方式效率比较
- 向文件中写入数据读取数据的几种方式
- asp.net 向xml 写入数据的几种方式
- C#中,向txt文本写入字符的几点技巧
- java向txt文件中写入数据时换行
- C#向excel中写入数据的三种方式
- 测试代码执行效率的几种方法比较
- 将字符串写入txt中
- java读写txt文档的几种方式
- 【转载】JAVA中字符串连接效率的测试
- C#测试代码执行效率的3种方式
- java将字符串写入到txt文件中
- Java学习笔记1:将字符串写入txt文件中
- java的io操作(将字符串写入到txt文件中)
- java的io操作(将字符串写入到txt文件中)
- SQL优化的思路及基本原则(mysql)
- spring boot自动配置
- Java 验证手机号码
- StoryBoard页面跳转方式 Show,Show Detail,Present Modally,Present as Popover
- vivado与modelsim的联合仿真(一)
- java 向txt中写入字符串的几种方式效率测试代码
- 彻底征服 Spring AOP 之 实战篇
- [book]《超越感觉》
- 小数进制转化 @ Python
- Android:Windows搭建Eclipse+JDK+SDK的Android
- 面试考察之Promise对象
- 使用ProcessOnh绘制流程图的过程中一些常见的问题
- 公司实施Odoo信息化系统Centos7下yum方式安装odoo (测试成功)
- 09. Yii 2.0 数据库交互 -读写分离