java7 NIO2(7) random access file API

java7NIO2 随机文件访问API


package com.mime;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.nio.channels.SeekableByteChannel;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.EnumSet;
import java.util.Set;

public class NIO2RandomAccessFile {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Path path = Paths.get("/tmp", "story.txt");

		// create the custom permissions attribute for the email.txt file
		Set<PosixFilePermission> perms = PosixFilePermissions
				.fromString("rw-r------");
		FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions
				.asFileAttribute(perms);

		// write a file using SeekableByteChannel
		try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(
				path, EnumSet.of(StandardOpenOption.WRITE,
						StandardOpenOption.TRUNCATE_EXISTING), attr)) {
			ByteBuffer buffer = ByteBuffer
					.wrap("Rafa Nadal produced another masterclass of clay-court tennis to win his fifth French Open title ..."
							.getBytes());
			int write = seekableByteChannel.write(buffer);
			System.out.println("Number of written bytes: " + write);
			buffer.clear();
		} catch (IOException ex) {
			System.err.println(ex);
		}

		// read a file using SeekableByteChannel
		try (SeekableByteChannel seekableByteChannel = Files.newByteChannel(
				path, EnumSet.of(StandardOpenOption.READ), attr)) {
			ByteBuffer buffer = ByteBuffer.allocate(12);
			String encoding = System.getProperty("file.encoding");
			buffer.clear();
			//随机访问定位API
//			seekableByteChannel.position();
//			seekableByteChannel.truncate(100);
			while (seekableByteChannel.read(buffer) > 0) {
				buffer.flip();
				System.out.print(Charset.forName(encoding).decode(buffer));
				buffer.clear();
			}

			// seekableByteChannel.position(seekableByteChannel.size()/2);

		} catch (IOException ex) {
			System.err.println(ex);
		}

		MappedByteBuffer buffer = null;
		try (FileChannel fileChannel = (FileChannel.open(path,
				EnumSet.of(StandardOpenOption.READ)))) {
			//文件锁
			FileLock lock = fileChannel.lock();
			buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0,
					fileChannel.size());
			//文件channel操作,从一个channel通道把数据传输到另外一个channel
//			fileChannel_from.transferTo(0L, fileChannel_from.size(), fileChannel_to);
			lock.release();
		} catch (IOException ex) {
			System.err.println(ex);
		}
		if (buffer != null) {
			try {
				Charset charset = Charset.defaultCharset();
				CharsetDecoder decoder = charset.newDecoder();
				CharBuffer charBuffer = decoder.decode(buffer);
				String content = charBuffer.toString();
				System.out.println(content);
				buffer.clear();
			} catch (CharacterCodingException ex) {
				System.err.println(ex);
			}
		}
	}
}


转载于:https://www.cnblogs.com/zhwj184/archive/2012/12/30/3027441.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值