java 向txt中写入字符串的几种方式效率测试代码

这个Java程序对比了使用PrintWriter、FileWriter、FileOutputStream、BufferedWriter不同方式向txt文件写入字符串的效率。结果显示,PrintWriter和BufferedWriter在效率上有优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

				
	}
 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值