|
| 1 | +package org.javacore.nio; |
| 2 | + |
| 3 | +import java.io.FileInputStream; |
| 4 | +import java.io.FileNotFoundException; |
| 5 | +import java.io.FileOutputStream; |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.ByteBuffer; |
| 8 | +import java.nio.channels.FileChannel; |
| 9 | +import java.nio.charset.Charset; |
| 10 | + |
| 11 | +/* |
| 12 | + * Copyright [2015] [Jeff Lee] |
| 13 | + * |
| 14 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 15 | + * you may not use this file except in compliance with the License. |
| 16 | + * You may obtain a copy of the License at |
| 17 | + * |
| 18 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | + * |
| 20 | + * Unless required by applicable law or agreed to in writing, software |
| 21 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 22 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | + * See the License for the specific language governing permissions and |
| 24 | + * limitations under the License. |
| 25 | + */ |
| 26 | + |
| 27 | +/** |
| 28 | + * @author Jeff Lee |
| 29 | + * @since 2015-10-10 18:58:15 |
| 30 | + * ByteBuffer与char之间转换的案例 |
| 31 | + */ |
| 32 | +public class BufferToText { |
| 33 | + private static final int BSIZE = 1024; // 1K 字节 |
| 34 | + public static void main(String[] args) throws IOException { |
| 35 | + // 从文件输出流获取FileChannel |
| 36 | + FileChannel fc = |
| 37 | + new FileOutputStream("data.txt").getChannel(); |
| 38 | + // 将带有字节的缓冲区写入该通道 |
| 39 | + fc.write(ByteBuffer.wrap("some data".getBytes())); |
| 40 | + fc.close(); |
| 41 | + |
| 42 | + // 从文件输入流中获取FileChannel |
| 43 | + fc = new FileInputStream("data.txt").getChannel(); |
| 44 | + // 分配ByteBuffer的大小 1K |
| 45 | + ByteBuffer buffer = ByteBuffer.allocate(BSIZE); |
| 46 | + // 将字节序列通过此通道写入到buffer |
| 47 | + fc.read(buffer); |
| 48 | + // 反转缓冲区,为写或者读取做准备 |
| 49 | + buffer.flip(); |
| 50 | + // 作为char缓冲区,并输出 乱码,因为Buffer编码是"UTF-16BE" |
| 51 | + System.out.println(buffer.asCharBuffer()); |
| 52 | + |
| 53 | + // 重绕此缓冲区 |
| 54 | + buffer.rewind(); |
| 55 | + // 获取文件编码属性 |
| 56 | + String encoding = System.getProperty("file.encoding"); |
| 57 | + // 输出编码及内容 |
| 58 | + System.out.println("编码: " + encoding + " 内容为: " |
| 59 | + + Charset.forName(encoding).decode(buffer)); |
| 60 | + fc.close(); |
| 61 | + |
| 62 | + // 从文件输出流获取FileChannel |
| 63 | + fc = new FileOutputStream("data.txt").getChannel(); |
| 64 | + // 将带有字节的缓冲区写入该通道 |
| 65 | + fc.write(ByteBuffer.wrap("some data".getBytes("UTF-16BE"))); |
| 66 | + fc.close(); |
| 67 | + |
| 68 | + // 从文件输入流中获取FileChannel |
| 69 | + fc = new FileInputStream("data.txt").getChannel(); |
| 70 | + // 清除其缓冲区 |
| 71 | + buffer.clear(); |
| 72 | + // 将字节序列通过该通道写入到buffer |
| 73 | + fc.read(buffer); |
| 74 | + // 反转缓冲区,为写或者读取做准备 |
| 75 | + buffer.flip(); |
| 76 | + // 作为char缓冲区,并输出 |
| 77 | + System.out.println(buffer.asCharBuffer()); |
| 78 | + fc.close(); |
| 79 | + |
| 80 | + // 从文件输出流获取FileChannel |
| 81 | + fc = new FileOutputStream("data.txt").getChannel(); |
| 82 | + // 指定buffer大小为 24字节 |
| 83 | + buffer = ByteBuffer.allocate(24); |
| 84 | + // 通过char缓冲区,在当前位置写入字符 |
| 85 | + buffer.asCharBuffer().put("some data"); |
| 86 | + // 将带有字节的缓冲区写入该通道 |
| 87 | + fc.write(buffer); |
| 88 | + fc.close(); |
| 89 | + |
| 90 | + // 从文件输入流中获取FileChannel |
| 91 | + fc = new FileInputStream("data.txt").getChannel(); |
| 92 | + // 清除缓冲区 |
| 93 | + buffer.clear(); |
| 94 | + // 将字节序列通过文件通道写入到buffer |
| 95 | + fc.read(buffer); |
| 96 | + // 反转缓冲区,为读或者写做准备 |
| 97 | + buffer.flip(); |
| 98 | + // 作为char缓冲区,并输出 |
| 99 | + System.out.println(buffer.asCharBuffer()); |
| 100 | + } |
| 101 | +} |
0 commit comments