Java IO操作之通道映射

2018-04-18  本文已影响40人  SUNOW2

通道映射

public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("...");
        try {
            FileChannel fileChannel = new FileInputStream(file).getChannel();
            MappedByteBuffer mappedByteBuffer = 
            fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
            Charset charset = Charset.forName("GBK");
            System.out.println(charset.decode(mappedByteBuffer));  // "hello world"
            // 将limit属性设置为当前的位置,回到缓冲区的第一个位置
            mappedByteBuffer.flip();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
上一篇 下一篇

猜你喜欢

热点阅读