java nio分段读文件

今天看到有同事blog中有个淘宝的笔试题也写了一个读大文件的东西,欢迎拍砖
题目是
   统计一个单词可重复的英文文件(假设4G)中每个单词出现的次数,把结果按照英文排序放入一个文件中。并能够检索特定单词的出现次数。由于文件过大,不重复单词总数有限,需要考虑到执行速度和内存使用情况。(淘宝笔试技术题)

	public  String readFile(String fName, long start, int len)
			throws Exception {
		RandomAccessFile raf = new RandomAccessFile(fName, "rw");
		byte src[] = new byte[len];
		// 文件读完返回为null
		if (raf.length() < start + len)
			return null;
		FileChannel channel = raf.getChannel();
		MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY,
				start, len);
		for (int i = 0; i < len; i++) {
			if (buffer.get(i) != 0) {
				src[i] = buffer.get(i);
			}
		}
		buffer.clear();
		channel.close();
		raf.close();
		return new String(src,0,len);
	}

IO读文件代码

	public  String[] readFile(String path, int form, int to)
			throws Exception {
		String strs[] = new String[to - form];
		FileReader fr = new FileReader(path);
		BufferedReader br = new BufferedReader(fr);
		String temp = null;
		int i = 0;
		while ((temp = br.readLine()) != null) {
			i++;
			if (i > form && i < to) {
				strs[i] = temp;
			}
		}
		br.close();
		fr.close();
		return strs;
	}

ps:代码中有很多bug  以前没有很多接触nio 欢迎指教!

猜你喜欢

转载自wuxing429.iteye.com/blog/1609558