当前位置: 移动技术网 > IT编程>开发语言>Java > Java文件读写IO/NIO及性能比较详细代码及总结

Java文件读写IO/NIO及性能比较详细代码及总结

2019年07月19日  | 移动技术网IT编程  | 我要评论

干java这么久,一直在做web相关的项目,一些基础类差不多都已经忘记。经常想得捡起,但总是因为一些原因,不能如愿。

其实不是没有时间,只是有些时候疲于总结,今得空,下定决心将丢掉的都给捡起来。

文件读写是一个在项目中经常遇到的工作,有些时候是因为维护,有些时候是新功能开发。我们的任务总是很重,工作节奏很快,快到我们不能停下脚步去总结。

文件读写有以下几种常用的方法

1、字节读写(inputstream/outputstream)

2、字符读取(filereader/filewriter)

3、行读取(bufferedreader/bufferedwriter)

代码(以读取为例):

import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.filereader;
import java.io.ioexception;
import java.io.inputstream;
/** 
 * <b>文件读取类</b><br /> 
 * 1、按字节读取文件内容<br /> 
 * 2、按字符读取文件内容<br /> 
 * 3、按行读取文件内容<br /> 
 * @author qin_xijuan 
 * 
 */
public class fileoperate {
	private static final string file_path = "d:/work/the list of beautiful music.txt";
	/** 
   * 以字节为单位读取文件内容 
   * @param filepath:需要读取的文件路径 
   */
	public static void readfilebybyte(string filepath) {
		file file = new file(filepath);
		// inputstream:此抽象类是表示字节输入流的所有类的超类。 
		inputstream ins = null ;
		try{
			// fileinputstream:从文件系统中的某个文件中获得输入字节。 
			ins = new fileinputstream(file);
			int temp ;
			// read():从输入流中读取数据的下一个字节。 
			while((temp = ins.read())!=-1){
				system.out.write(temp);
			}
		}
		catch(exception e){
			e.getstacktrace();
		}
		finally{
			if (ins != null){
				try{
					ins.close();
				}
				catch(ioexception e){
					e.getstacktrace();
				}
			}
		}
	}
	/** 
   * 以字符为单位读取文件内容 
   * @param filepath 
   */
	public static void readfilebycharacter(string filepath){
		file file = new file(filepath);
		// filereader:用来读取字符文件的便捷类。 
		filereader reader = null;
		try{
			reader = new filereader(file);
			int temp ;
			while((temp = reader.read()) != -1){
				if (((char) temp) != '\r') {
					system.out.print((char) temp);
				}
			}
		}
		catch(ioexception e){
			e.getstacktrace();
		}
		finally{
			if (reader != null){
				try {
					reader.close();
				}
				catch (ioexception e) {
					e.printstacktrace();
				}
			}
		}
	}
	/** 
   * 以行为单位读取文件内容 
   * @param filepath 
   */
	public static void readfilebyline(string filepath){
		file file = new file(filepath);
		// bufferedreader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 
		bufferedreader buf = null;
		try{
			// filereader:用来读取字符文件的便捷类。 
			buf = new bufferedreader(new filereader(file));
			// buf = new bufferedreader(new inputstreamreader(new fileinputstream(file))); 
			string temp = null ;
			while ((temp = buf.readline()) != null ){
				system.out.println(temp);
			}
		}
		catch(exception e){
			e.getstacktrace();
		}
		finally{
			if(buf != null){
				try{
					buf.close();
				}
				catch (ioexception e) {
					e.getstacktrace();
				}
			}
		}
	}
	public static void main(string args[]) {
		readfilebybyte(file_path);
		readfilebycharacter(file_path);
		readfilebyline(file_path);
	}
}

//-----------------------------------------------------------------分割线-----------------------------------------------------------------------------

再经过两位同行的提点下,我对之前写的文件做了点修改,并通过读写一个1.2m的文本文件来测试各方法的性能。从多次测试结果来看,行读写却是是java.nio更有效率。

经过修改之后的代码如下:

package com.waddell.basic;
import java.io.bufferedreader;
import java.io.bufferedwriter;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.filereader;
import java.io.filewriter;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.nio.bytebuffer;
import java.nio.channels.filechannel;
/** 
 * <b>文件读取类</b><br /> 
 * 1、按字节读取文件内容<br /> 
 * 2、按字符读取文件内容<br /> 
 * 3、按行读取文件内容<br /> 
 * 
 * @author qin_xijuan 
 * 
 */
public class fileoperate {
	private static final string file_path = "d:/work/jipinwodi.txt";
	/** 
   * 以字节为单位读写文件内容 
   * 
   * @param filepath 
   *      :需要读取的文件路径 
   */
	public static void readfilebybyte(string filepath) {
		file file = new file(filepath);
		// inputstream:此抽象类是表示字节输入流的所有类的超类。 
		inputstream ins = null;
		outputstream outs = null;
		try {
			// fileinputstream:从文件系统中的某个文件中获得输入字节。 
			ins = new fileinputstream(file);
			outs = new fileoutputstream("d:/work/readfilebybyte.txt");
			int temp;
			// read():从输入流中读取数据的下一个字节。 
			while ((temp = ins.read()) != -1) {
				outs.write(temp);
			}
		}
		catch (exception e) {
			e.getstacktrace();
		}
		finally {
			if (ins != null && outs != null) {
				try {
					outs.close();
					ins.close();
				}
				catch (ioexception e) {
					e.getstacktrace();
				}
			}
		}
	}
	/** 
   * 以字符为单位读写文件内容 
   * 
   * @param filepath 
   */
	public static void readfilebycharacter(string filepath) {
		file file = new file(filepath);
		// filereader:用来读取字符文件的便捷类。 
		filereader reader = null;
		filewriter writer = null;
		try {
			reader = new filereader(file);
			writer = new filewriter("d:/work/readfilebycharacter.txt");
			int temp;
			while ((temp = reader.read()) != -1) {
				writer.write((char)temp);
			}
		}
		catch (ioexception e) {
			e.getstacktrace();
		}
		finally {
			if (reader != null && writer != null) {
				try {
					reader.close();
					writer.close();
				}
				catch (ioexception e) {
					e.printstacktrace();
				}
			}
		}
	}
	/** 
   * 以行为单位读写文件内容 
   * 
   * @param filepath 
   */
	public static void readfilebyline(string filepath) {
		file file = new file(filepath);
		// bufferedreader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。 
		bufferedreader bufreader = null;
		bufferedwriter bufwriter = null;
		try {
			// filereader:用来读取字符文件的便捷类。 
			bufreader = new bufferedreader(new filereader(file));
			bufwriter = new bufferedwriter(new filewriter("d:/work/readfilebyline.txt"));
			// buf = new bufferedreader(new inputstreamreader(new 
			// fileinputstream(file))); 
			string temp = null;
			while ((temp = bufreader.readline()) != null) {
				bufwriter.write(temp+"\n");
			}
		}
		catch (exception e) {
			e.getstacktrace();
		}
		finally {
			if (bufreader != null && bufwriter != null) {
				try {
					bufreader.close();
					bufwriter.close();
				}
				catch (ioexception e) {
					e.getstacktrace();
				}
			}
		}
	}
	/** 
   * 使用java.nio bytebuffer字节将一个文件输出至另一文件 
   * 
   * @param filepath 
   */
	public static void readfilebybybebuffer(string filepath) {
		fileinputstream in = null;
		fileoutputstream out = null;
		try {
			// 获取源文件和目标文件的输入输出流  
			in = new fileinputstream(filepath);
			out = new fileoutputstream("d:/work/readfilebybybebuffer.txt");
			// 获取输入输出通道 
			filechannel fcin = in.getchannel();
			filechannel fcout = out.getchannel();
			bytebuffer buffer = bytebuffer.allocate(1024);
			while (true) {
				// clear方法重设缓冲区,使它可以接受读入的数据 
				buffer.clear();
				// 从输入通道中将数据读到缓冲区 
				int r = fcin.read(buffer);
				if (r == -1) {
					break;
				}
				// flip方法让缓冲区可以将新读入的数据写入另一个通道  
				buffer.flip();
				fcout.write(buffer);
			}
		}
		catch (exception e) {
			e.printstacktrace();
		}
		finally {
			if (in != null && out != null) {
				try {
					in.close();
					out.close();
				}
				catch (ioexception e) {
					e.printstacktrace();
				}
			}
		}
	}
	public static long gettime(){
		return system.currenttimemillis();
	}
	public static void main(string args[]) {
		long time1 = gettime() ;
		// readfilebybyte(file_path);// 8734,8281,8000,7781,8047 
		// readfilebycharacter(file_path);// 734, 437, 437, 438, 422 
		// readfilebyline(file_path);// 110, 94, 94, 110, 93 
		readfilebybybebuffer(file_path);
		// 125, 78, 62, 78, 62 
		long time2 = gettime() ;
		system.out.println(time2-time1);
	}
}

在main方法中,调用各方法之后,有五组数据,分辨是我5次读写文件测试出来的时间(毫秒)。

关于java.nio请参考:

个人测试:

public static void main(string args[]) {
	long time1 = gettime() ;
	//     readfilebybyte(file_path);   //2338,2286 
	//     readfilebycharacter(file_path);//160,162,158 
	//     readfilebyline(file_path);   //46,51,57 
	//    readfilebybybebuffer(file_path);//19,18,17 
	//    readfilebybybebuffer(file_path);//2048: 11,13 
	//    readfilebybybebuffer(file_path);//1024*100 100k,711k: 6,6 
	//    readfilebybybebuffer(file_path);//1024*100 100k,1422k: 7 
	//    readfilebybybebuffer(file_path);//1024*100 100k,9951k: 49,48 
	//    readfilebybybebuffer(file_path);//1024*1000 1m,711k: 7,7 
	//    readfilebybybebuffer(file_path);//1024*1000 1m,1422k: 7,8 
	//    readfilebybybebuffer(file_path);//1024*1000 1m,9951k: 48,49 
	//    readfilebybybebuffer(file_path);//1024*10000 10m,711k: 21,13,17 
	//    readfilebybybebuffer(file_path);//1024*10000 10m,1422k: 16,17,14,15 
	//    readfilebybybebuffer(file_path);//1024*10000 10m,9951k:64,60 
	long time2 = gettime() ;
	system.out.println(time2-time1);
}

总结

以上就是本文关于java文件读写io/nio及性能比较详细代码及总结的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网