四、通道

1.通道的原理和获取

/**
 * 1. 利用通道完成文件的复制.(非直接缓存区)
 */
@Test
public void test01() throws IOException {
    FileInputStream fileInputStream = new FileInputStream("1.jpeg");
    FileOutputStream fileOutputStream = new FileOutputStream("2.jpg");

    // 1️⃣获取通道
    FileChannel inFileChannel = fileInputStream.getChannel();
    FileChannel outFileChannel = fileOutputStream.getChannel();
    // 2️⃣分配指定大小的缓存区
    ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
    // 3️⃣将通道中的数据存入缓存区中
    while (inFileChannel.read(byteBuffer) != -1) {
        // 4️⃣将缓存区中的数据写入通道中
        // 切换成读数据模式
        byteBuffer.flip();
        outFileChannel.write(byteBuffer);
        // 清空缓存区
        byteBuffer.clear();
    }
    outFileChannel.close();
    inFileChannel.close();
    fileOutputStream.close();
    fileInputStream.close();
}

2.通道数据传输与内存映射文件

 

/**
 * 2. 使用直接缓存区完成文件的复制(内存映射文件)
 * 只有btteBuffer支持
 */
@Test
public void test2() throws IOException {
    FileChannel inChannel = FileChannel.open(Paths.get("1.jpeg"), StandardOpenOption.READ);
    FileChannel outChannel = FileChannel.open(Paths.get("3.jpg"), StandardOpenOption.WRITE, StandardOpenOption.READ,
            StandardOpenOption.CREATE);

    // 内存映射文件
    MappedByteBuffer inMapBuffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
    // 因为这里是读写模式(FileChannel.MapMode.READ_WRITE),所以,上面的outChannel,也加上读模式
    MappedByteBuffer outMapBuffer = outChannel.map(FileChannel.MapMode.READ_WRITE, 0, inChannel.size());

    // 直接对缓存区进行数据的读写操作
    byte[] bytes = new byte[inMapBuffer.limit()];
    inMapBuffer.get(bytes);
    outMapBuffer.put(bytes);

    inChannel.close();
    outChannel.close();

}

3、分散(Scatter)与聚集(Gather)

 

分散读取(Scattering Reads): 将通道中的数据分散到多个缓冲区中

聚集写入(Gathering Writes):将多个缓存区中的数据聚集到通道中

/**
 * 分散和聚集
 */
@Test
public void test4() throws IOException {
    RandomAccessFile randomAccessFile = new RandomAccessFile("1.text","rw");

    // 1. 获取通道
    FileChannel channel = randomAccessFile.getChannel();

    // 2. 分配指定大小的缓冲区
    ByteBuffer byteBuffer = ByteBuffer.allocate(100);
    ByteBuffer byteBuffer1 = ByteBuffer.allocate(1024);

    // 3. 分散读取
    ByteBuffer[] byteBuffers = {byteBuffer, byteBuffer1};
    channel.read(byteBuffers);

    for (ByteBuffer byteBuffer2: byteBuffers) {
        byteBuffer2.flip();
    }
    System.out.println(new String(byteBuffers[0].array(),0, byteBuffers[0].limit()));
    System.out.println("------------------");
    System.out.println(new String(byteBuffers[1].array(),0,byteBuffers[1].limit()));

    // 4. 聚集写入
    RandomAccessFile randomAccessFile1 = new RandomAccessFile("2.text", "rw");
    FileChannel fileChannel = randomAccessFile1.getChannel();
    fileChannel.write(byteBuffers);
}

4.字符集:Charset

编码:字符串 =》 字节数组

解码:字节数组 =》 字符串

/**
 * 编码与解码
 * @throws CharacterCodingException
 */
@Test
public void test6() throws CharacterCodingException {
    Charset charset = Charset.forName("GBK");

    // 获取编码器
    CharsetEncoder charsetEncoder = charset.newEncoder();
    // 获取解码器
    CharsetDecoder charsetDecoder = charset.newDecoder();

    CharBuffer charBuffer = CharBuffer.allocate(1024);
    charBuffer.put("快放假!!!");
    charBuffer.flip();

    // 编码
    ByteBuffer byteBuffer = charsetEncoder.encode(charBuffer);

    for (int i = 0; i < 12; i++) {
        System.out.println(byteBuffer.get());
    }

    byteBuffer.flip();
    // 解码
    CharBuffer decode = charsetDecoder.decode(byteBuffer);

    System.out.println("解码后:" + decode.toString());

    System.out.println("------------------");
    Charset charset1 = Charset.forName("UTF-8");
    byteBuffer.flip();
    CharBuffer charBuffer1 = charsetDecoder.decode(byteBuffer);
    System.out.println("====================" + charBuffer1);

}

/**
 * 字符集。
 */
@Test
public void test5() {
    SortedMap<String, Charset> stringCharsetSortedMap = Charset.availableCharsets();
    Set<Map.Entry<String, Charset>> entries = stringCharsetSortedMap.entrySet();

    for (Map.Entry<String, Charset> entry : entries) {
        System.out.println(entry.getKey() + "=" + entry.getValue());
    }
}

 

学自,B站尚硅谷

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值