`

Java读取大文件,超越内存限制

阅读更多
// 经典读取数据三(大文件)
	public static void readerFile3(String path) {
		int length = 0x8FFFFFF; // 128 Mb
		try {
			MappedByteBuffer out = new RandomAccessFile(path, "r").getChannel()
					.map(FileChannel.MapMode.READ_ONLY, 0, length);
			String result = "";
			for (int i = 0; i < 130; i++) {
				if (out.get(i) == 10) {
					result = "";
					System.out.println("换行了。");
				} else {
					result += (char) out.get(i);
				}
				if (out.get(i) == 13) {
					System.out.println(result + ":" + (result.trim().length()));
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics