IO读写文件的简单性能比较

public class InputStreamTest {

	public static void main(String[] args) {
		InputStreamTest inputstream = new InputStreamTest();
		FileInputStreamExample example = inputstream.new FileInputStreamExample("D:\\istream\\rechargeLarge.txt", "D:\\istream\\rechargeCopy.txt");
		long start = System.currentTimeMillis();
//		example.copy();
		example.copyReader();
//		example.copyNio();
		System.out.println(System.currentTimeMillis() - start);
	}
	
	public class FileInputStreamExample {
		
		String inputPath;
		String outputPath;
		
		public FileInputStreamExample(String inputPath, String outputPath) {
			this.inputPath = inputPath;
			this.outputPath = outputPath;
		}
		
		public FileInputStreamExample(String inputPath) {
			this(inputPath, null);
		}
		
		public void copy() {
			FileInputStream inputStream = null;
			BufferedInputStream bufferInputStream = null;
			FileOutputStream outputStream = null;
			BufferedOutputStream bufferOutputStream = null;
			try {
				inputStream = new FileInputStream(inputPath);
				bufferInputStream = new BufferedInputStream(inputStream);
				outputStream = new FileOutputStream(outputPath);
				bufferOutputStream = new BufferedOutputStream(outputStream);
				
				int n = 0;
				byte[] but = new byte[1024];
				while((n = bufferInputStream.read(but)) != -1) {
					//这里注意读到多少写多少
					bufferOutputStream.write(but, 0, n);
				}
				bufferOutputStream.flush();
				
//				System.out.println("再一次读" + inputStream.read());
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					bufferInputStream.close();
					inputStream.close();
					bufferOutputStream.close();
					outputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		public void copyReader() {
			FileReader fileReader = null;
			BufferedReader bufferedReader = null;
			FileWriter fileWriter = null;
			BufferedWriter bufferedWriter = null;
			try {
				fileReader = new FileReader(inputPath);
				bufferedReader = new BufferedReader(fileReader);
				fileWriter = new FileWriter(outputPath);
				bufferedWriter = new BufferedWriter(fileWriter);
				
				int n = 0;
				char[] buf = new char[1024];
				while ((n = bufferedReader.read(buf)) != -1) {
					bufferedWriter.write(buf, 0, n);
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					bufferedReader.close();
					fileReader.close();
					bufferedWriter.close();
					fileWriter.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		public void copyNio() {
			FileInputStream inputStream = null;
			FileOutputStream outputStream = null;
			FileChannel inputChannel = null;
			FileChannel outputChannel = null;
			try {
				inputStream = new FileInputStream(inputPath);
				outputStream = new FileOutputStream(outputPath);
				inputChannel = inputStream.getChannel();
				outputChannel = outputStream.getChannel();
				
				ByteBuffer buffer = ByteBuffer.allocate(1024);
				while(inputChannel.read(buffer) != -1) {
					buffer.flip();
					outputChannel.write(buffer);
					buffer.clear();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				try {
					inputStream.close();
					outputStream.close();
					inputChannel.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

 

    在windows7平台下,做了三种IO例子的简单性能比较。通过对比发现,对于文件IO,NIO并不比流快,速度受缓冲区大小的影响,在同样缓冲区大小的条件下,按字节读写流的速度反而比NIO的速度更快。对于字符流,由于其构建在字节流的基础上,需要处理编码信息,其读写速度要比字节流慢。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值