java 向txt中写入字符串的几种方式效率测试代码
2017-09-06 11:17
447 查看
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 block e.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 block e.printStackTrace(); }catch (IOException e) { // TODO Auto-generated catch block e.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 block e.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 block e.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 block e.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 block e.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 block e.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 block e.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()); } }
相关文章推荐
- java向txt中写入字符串的几种方式以及效率
- java读取写入文件几种方式效率比较
- [测试]java IO写入文件效率——几种方法比较
- java IO写入文件效率——几种方法比较
- JAVA中字符串连接效率的测试
- Java IO读写大文件的几种方式及测试
- java application应用程序 使用JDBC和proxool两种方式连接数据库 的测试程序代码
- Java几种拼接字符串的效率问题
- Java IO读写大文件的几种方式及测试
- 大文件字符串搜索之Java函数和调用Shell搜索的效率测试
- Java IO读写大文件的几种方式及测试
- 【Java】—— java Web 启动时自动执行代码的几种方式(总有些代码需要在虚拟机启动时执行)
- java中操作xml几种方式的比较和代码示例(DOM,SAX,JDOM,DOM4J)
- Java IO读写大文件的几种方式及测试
- 提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择
- java application 应用程序 使用JDBC proxool 两种方式连接数据库 测试程序代码
- JS下高效拼装字符串的几种方法比较与测试代码
- 提高你的Java代码质量吧:让我们疑惑的字符串拼接方式的选择
- tomcat免重启随意更改java代码 提高开发效率(未测试)
- 将包中的所有java源文件代码写入一个txt文件中