RandomAccessFile使用示例

	public static void method1() {
		RandomAccessFile aFile = null;
		try {
			aFile = new RandomAccessFile("D:\\\\var\\\\demo.txt", "rw");
			FileChannel fileChannel = aFile.getChannel();
			ByteBuffer buf = ByteBuffer.allocate(1024);
			int bytesRead = fileChannel.read(buf);
			System.out.println(bytesRead);
			while (bytesRead != -1) {
				buf.flip();
				while (buf.hasRemaining()) {
					System.out.print((char) buf.get());
				}
				buf.compact();
				bytesRead = fileChannel.read(buf);
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (aFile != null) {
					aFile.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
发布了176 篇原创文章 · 获赞 1 · 访问量 7153

猜你喜欢

转载自blog.csdn.net/qq_37769323/article/details/104194751