在劫

吾生也有涯,而知也无涯 。

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
	RandomAccessFile aFile = new RandomAccessFile("data/nio-data.txt", "rw");
	FileChannel inChannel = aFile.getChannel();
	ByteBuffer buf = ByteBuffer.allocate(48);
	int bytesRead = inChannel.read(buf);
	while (bytesRead != -1) {
		System.out.println("Read " + bytesRead);
		//首先读取数据的Buffer,然后反转Buffer,再从Buffer中读取数据
		buf.flip();
		while(buf.hasRemaining()){
			System.out.print((char) buf.get());
		}
		buf.clear();
		bytesRead = inChannel.read(buf);
	}
	aFile.close();


posted on 2018-03-15 09:32  长嘴大耳怪  阅读(97)  评论(0编辑  收藏  举报