疑问:调用Java NIO提高文件读写速度

在51cto上看到一篇文章 调用Java NIO提高文件读写速度http://developer.51cto.com/art/201111/302489.htm

说调用Java NIO提高文件读写速度

因此很好奇,然后自己写了测试类,代码如下:

import java.io.*;

import java.nio.*;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class ReadFileTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String filePath = "F:\\23Test\\wls1034_oepe111161_linux32.bin";
		Long sTime;
		Long eTime;
		ReadFileTest r = new ReadFileTest();

		// item1
		sTime = System.currentTimeMillis();
		r.readFileIO(filePath);
		eTime = System.currentTimeMillis();
		System.out.println("IO耗时:" + (eTime - sTime));

		// item2
		sTime = System.currentTimeMillis();
		r.readFileNIO(filePath);
		eTime = System.currentTimeMillis();
		System.out.println("NIO耗时:" + (eTime - sTime));

	}

	private void readFileIO(String filePath) {
		String outFile = "F:\\23Test\\io-bak.bin";
		File inFile = new File(filePath);
		int bytesum = 0;
		int byteread = 0;
		if (inFile.exists()) {
			FileInputStream inStream;
			FileOutputStream fs;
			try {
				inStream = new FileInputStream(inFile);
				fs = new FileOutputStream(outFile);
				byte[] buffer = new byte[1024];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread;
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				fs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

	private void readFileNIO(String filePath) {
		String outFile = "F:\\23Test\\nio-bak.bin";
		File inFile = new File(filePath);
		try {
			FileInputStream inf = new FileInputStream(inFile);
			FileOutputStream outf = new FileOutputStream(outFile);
			ByteBuffer buffer = ByteBuffer.allocate(1024);
			// 准备文件读取的管道-->相当于烟仓和烟管
			FileChannel inFc = inf.getChannel();
			FileChannel outFc = outf.getChannel();
			//Charset charSet = Charset.forName("utf-8");
			// 进行编码解码-->相当于水斗中水的过滤作用
			//CharsetDecoder decoder = charSet.newDecoder();
			//CharsetEncoder encoder = charSet.newEncoder();
			while (true) {
				// 准备向Buffer中写入数据-->相当于点燃烟丝,完事具备只欠东风
				buffer.clear();
				// 进行字符编码 -->相当于水的过滤作用
				//CharBuffer cb = decoder.decode(buffer);
				//ByteBuffer bb = encoder.encode(cb);

				// 数据经过编码以后暂存缓冲区-->相当于经过水过滤后的烟暂停在水斗中
				int t = inFc.read(buffer);
				if (t == -1) {
					break;
				}
				buffer.flip();
				//bb.flip();
				// 将字节码写入目标文件-->相当于烟已经进入到嘴里
				outFc.write(buffer);
			}
			inFc.close();
			outFc.close();
			inf.close();
			outf.close();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
结果如下 :

第一轮测试

IO耗时:36597
NIO耗时:46376

第二轮测试

IO耗时:35492
NIO耗时:43809

第三轮测试

IO耗时:49827
NIO耗时:50920

经过几轮测试下来,我发现用NIO通道的,效率没有以前代码的写法效率高,第次都比较慢

所以我认为用他说的这种方式并没有提高访问文件的速度

请有知道的高人能够给我解惑?



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值