Channel与普通的流存在的差别是:Channel可以将文件的全部或者部分直接映射成为Buffer,程序不能直接访问Channel中的数据(读、写),只能通过Buffer来与Channel交互。

Channel是通过其功能进行划分的,如:DatagramChannel(UDP网络通信)、Pipe.sinkChannel(线程通信)......

    public static void main(String[] args) {
        File file = new File("src/threaddemo/FileChannelTest.java");
        try (FileChannel inChannel = new FileInputStream(file).getChannel();
                FileChannel outChannel = new FileOutputStream("test.txt")
                        .getChannel()) {
            MappedByteBuffer buff = inChannel.map(
                    FileChannel.MapMode.READ_ONLY, 0, file.length());
            Charset charset = Charset.forName("GBK");
            outChannel.write(buff);
            buff.clear();
            CharsetDecoder decoder = charset.newDecoder();
            CharBuffer charBuffer = decoder.decode(buff);
            System.out.println(charBuffer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}