java 文件读写操作

java文件读写速度简单比较:

package text1;
import java.io.BufferedOutputStream;   
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;   
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;   
import java.io.FileReader;
import java.io.FileWriter;   
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.StringReader;
import java.nio.ByteBuffer;   
import java.nio.channels.FileChannel;   

public class java文件读取与写入练习 {
	public static void main(String[] args) throws IOException {
		System.out.println("写入文件中……");
		Writer();
		System.out.println("读取文件中……");
		Reader();
	}   
	public static void Writer(){//读入方式的比较	
		FileOutputStream out = null;   
	    FileOutputStream outSTr = null;   
	    BufferedOutputStream Buff = null;   
	    FileWriter fw = null;   
	    int count = 1000000;// 写文件行数   
	    try {   
	    	byte[] bs = "java的输入操作比较!!!\r\n".getBytes();   
	      	out = new FileOutputStream(new File("d:/add.txt")); 
	      	//FileOutputStream的读入操作
	      	long begin = System.currentTimeMillis();//标记开始时间   
	      	for (int i = 0; i < count; i++) {   
	    	  out.write(bs);   
	      	}   
	      	out.close();   
	      	long end = System.currentTimeMillis(); //标记结束时间  
	      	System.out.println("FileOutputStream执行耗时:" + (end - begin) + " 豪秒");   
	      
	      	//BufferedOutputStream的读入操作
	      	outSTr = new FileOutputStream(new File("d:/add0.txt"));   
	      	Buff = new BufferedOutputStream(outSTr);  
	      	long begin0 = System.currentTimeMillis();   
	      	for (int i = 0; i < count; i++) {   
	    	  Buff.write(bs);   
	      	}   
	      	Buff.flush();   
	      	Buff.close();   
	      	long end0 = System.currentTimeMillis();   
	      	System.out.println("BufferedOutputStream执行耗时:" + (end0 - begin0) + " 豪秒");  
  
		  	//FileWriter的读入操作
	      	fw = new FileWriter("d:/add2.txt");   
	      	long begin3 = System.currentTimeMillis();   
	      	for (int i = 0; i < count; i++) {   
	    	  fw.write("java的输入操作比较!!!\r\n");   
	      	}   
	      	fw.flush();   
	      	fw.close();   
	      	long end3 = System.currentTimeMillis();   
	      	System.out.println("FileWriter执行耗时:" + (end3 - begin3) + " 豪秒");   
	      
	      
	      	//FileChannel的读入操作
	      	long begin4 = System.currentTimeMillis();   
	      	String path = "d:/add3.txt";   
	      	ByteBuffer bb = ByteBuffer.wrap(bs);   
	      	FileChannel out2 = new FileOutputStream(path).getChannel();   
	      	for (int i = 0; i < count; i++) {   
	    	  out2.write(bb);   
	    	  bb.rewind();   
	        }   
      		out2.close();   
      		long end4 = System.currentTimeMillis();   
      		System.out.println("FileChannel执行耗时:" + (end4 - begin4) + " 豪秒"); 
      		//PrintWriter的读入
      		PrintWriter out1= new PrintWriter(new File("d:/add4.txt")); 
      		long begin5 = System.currentTimeMillis(); 
            for (int i = 0; i < count; i++){  
                out1.println(bs);    
            }   
            out1.close();   
            long end5 = System.currentTimeMillis();
            System.out.println("PrintWriter执行耗时:" + (end5 - begin5) + " 豪秒"); 
	    }catch(Exception e){   
	    	e.printStackTrace();   
	    }finally {   
	    	try{   
		        fw.close();   
		        Buff.close();   
		        outSTr.close();   
		        out.close();   
	    	}catch (Exception e) {   
	    		e.printStackTrace();   
		   }   
	    }
	    //BufferedWriter的读入
		BufferedWriter bw = null;
		long begin6 = System.currentTimeMillis();
		try{
			//通过System.in返回一个InputStream对象用于构造一个InputStreamReader对象
			bw = new BufferedWriter(new FileWriter(new File("d:/add5.txt"),true));//有内容写入尾部 否则不写   //true表示是否追加
			for (int i = 0; i < count; i++){	//如果用户输入exit则退出循环
				bw.write("java的输入操作比较!!!\r\n");			//将用户输入的字符串写入文件
				//bw.newLine();			//换行 
				bw.flush();		        //刷新缓冲区,将缓冲区的字符写入磁盘!	//继续接收输入
			}
		}
		catch(FileNotFoundException e){
			System.out.println(e.getMessage());
		}
		catch(IOException e){
			System.out.println(e.getMessage());
		}
		finally{
			try {
				bw.close();		//关闭对象前会调用bw.flush();
				long end6 = System.currentTimeMillis(); 
				System.out.println("BufferedWriter执行耗时:" + (end6 - begin6) + " 豪秒"); 
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	   
	}
	public static void Reader() throws IOException{
		//FileInputStream
		File file = new File("d:/add.txt"); 
		long begin1 = System.currentTimeMillis();//标记开始时间   
	       InputStream in = null;  
	       try {   
	           // 一次读一个字节  
	           in = new FileInputStream(file);  
	           int tempbyte;  
	           while ((tempbyte = in.read())!=-1) {  
	               //System.out.println(tempbyte);  
	           } 
	           long end1 = System.currentTimeMillis();
	           System.out.print("FileInputStream执行耗时:" + (end1 - begin1) + " 豪秒"+"   "); 
	       }catch (Exception e) {   
	           e.printStackTrace();  
	       }  
	       long begin2 = System.currentTimeMillis();//标记开始时间     
	       try{  
	           //System.out.println("以字节为单位读取文件内容,一次读多个字节:");  
	           // 一次读多个字节  
	    	    
	           byte[] tempbytes = new byte[100];  
	           int byteread = 0;  
	           in = new FileInputStream("d:/add.txt");  
	           //ReadFromFile.showAvailableBytes(in);  
	           // 读入多个字节到字节数组中,byteread为一次读入的字节数  
	           while ((byteread = in.read(tempbytes)) != -1) {  
	               //System.out.write(tempbytes, 0, byteread);//好方法,第一个参数是数组,第二个参数是开始位置,第三个参数是长度  
	           }  
	       }catch (Exception e1){  
	            e1.printStackTrace();  
	       }finally{  
	           if (in != null){  
	               try {  
	                  in.close();
	                  long end2 = System.currentTimeMillis();
	   	           	  System.out.println((end2 - begin2) + " 豪秒");
	               } catch (IOException e1) {  
	               }  
	           }  
	       }
	       
	       //InputStreamReader读取 
	        Reader reader = null; 
	        long begin3 = System.currentTimeMillis();//标记开始时间     
	        try {  
	            //System.out.println("以字符为单位读取文件内容,一次读一个字节:");  
	            // 一次读一个字符  
	            reader = new InputStreamReader(new FileInputStream(file));  
	            int tempchar;  
	            while ((tempchar = reader.read()) != -1) {  
	                // 对于windows下,\r\n这两个字符在一起时,表示一个换行。  
	                // 但如果这两个字符分开显示时,会换两次行。  
	                // 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。  
	                if (((char) tempchar) != '\r') {  
	                    //System.out.print((char) tempchar);  
	                }  
	            }  
	            reader.close(); 
	            long end3 = System.currentTimeMillis();
 	           	System.out.println("InputStreamReader执行耗时:" + (end3 - begin3) + " 豪秒");
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        } 
	        long begin4 = System.currentTimeMillis();//标记开始时间   
	        try {  
	            //System.out.println("\n以字符为单位读取文件内容,一次读多个字节:");  
	            // 一次读多个字符  
	            char[] tempchars = new char[30];  
	            int charread = 0;  
	            reader = new InputStreamReader(new FileInputStream("d:/add.txt"));  
	            // 读入多个字符到字符数组中,charread为一次读取字符数  
	            while ((charread = reader.read(tempchars)) != -1) {  
	                // 同样屏蔽掉\r不显示  
	                if ((charread == tempchars.length)  
	                        && (tempchars[tempchars.length - 1] != '\r')) {  
	                    //System.out.print(tempchars);  
	                } else {  
	                    for (int i = 0; i < charread; i++) {  
	                        if (tempchars[i] == '\r') {  
	                            continue;  
	                        } else {  
	                            //System.out.print(tempchars[i]);  
	                        }  
	                    }  
	                }  
	            }  
	  
	        } catch (Exception e1) {  
	            e1.printStackTrace();  
	        } finally {  
	            if (reader != null) {  
	                try {  
	                    reader.close(); 
	                    long end4 = System.currentTimeMillis();
	     	           	System.out.println("InputStreamReader执行耗时:" + (end4 - begin4) + " 豪秒");
	                } catch (IOException e1) {  
	                }  
	            }  
	        }  
	       //BufferedReader的读取
	        BufferedReader reader1 = null; 
	        long begin5 = System.currentTimeMillis();//标记开始时间     
	        try {  
	            //System.out.println("以行为单位读取文件内容,一次读一整行:");  
	            reader1 = new BufferedReader(new FileReader(file));  
	            String tempString = null;  
	            int line = 1;  
	            // 一次读入一行,直到读入null为文件结束  
	            while ((tempString = reader1.readLine()) != null) {  
	                // 显示行号  
	                //System.out.println("line " + line + ": " + tempString);  
	                line++;  
	            }  
	            reader1.close();  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } finally {  
	            if (reader != null) {  
	                try {  
	                    reader1.close(); 
	                    long end5 = System.currentTimeMillis();
	     	           	System.out.println("BufferedReader执行耗时:" + (end5 - begin5) + " 豪秒");
	                } catch (IOException e1) {  
	                }  
	            }  
	        } 
	       //RandomAccessFile读取
	        RandomAccessFile randomFile = null; 
	        long begin6 = System.currentTimeMillis();//标记开始时间 
	        try {  
	            //System.out.println("随机读取一段文件内容:");  
	            // 打开一个随机访问文件流,按只读方式  
	            randomFile = new RandomAccessFile("d:/add.txt", "r");  
	            // 文件长度,字节数  
	            long fileLength = randomFile.length();  
	            // 读文件的起始位置  
	            int beginIndex = (fileLength > 4) ? 0 : 0;  
	            // 将读文件的开始位置移到beginIndex位置。  
	            randomFile.seek(beginIndex);  
	            byte[] bytes = new byte[10];  
	            int byteread = 0;  
	            // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。  
	            // 将一次读取的字节数赋给byteread  
	            while ((byteread = randomFile.read(bytes)) != -1) {  
	                //System.out.write(bytes, 0, byteread);  
	            }  
	        } catch (IOException e) {  
	            e.printStackTrace();  
	        } finally {  
	            if (randomFile != null) {  
	                try {  
	                    randomFile.close(); 
	                    long end6 = System.currentTimeMillis();
	     	           	System.out.println("RandomAccessFile执行耗时:" + (end6 - begin6) + " 豪秒");
	                } catch (IOException e1) {  
	                }  
	            }  
	        }
	}
}

运行结果如下:

写入文件中……
FileOutputStream执行耗时:3594 豪秒
BufferedOutputStream执行耗时:328 豪秒
FileWriter执行耗时:422 豪秒
FileChannel执行耗时:4672 豪秒
PrintWriter执行耗时:422 豪秒
BufferedWriter执行耗时:6984 豪秒
读取文件中……
FileInputStream执行耗时:34750 豪秒   406 豪秒
InputStreamReader执行耗时:1094 豪秒
InputStreamReader执行耗时:266 豪秒
BufferedReader执行耗时:312 豪秒
RandomAccessFile执行耗时:3781 豪秒

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值