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());} }

原创粉丝点击