国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

SeekableByteChannel 和 FileChannel

2019-11-15 00:04:20
字體:
來源:轉載
供稿:網友
SeekableByteChannel 和 FileChannel

java7中引入了SeekableByteChannel接口,允許我們定位到文件的任意位置進行讀寫。注意這里的寫,不是新增式的插入,而是覆蓋,當然在文件末尾的寫,是新增。

java.nio.channels.SeekableByteChannelA byte channel that maintains a current position and allows the position to be changed. A seekable byte channel is connected to an entity, typically a file, that contains a variable-length sequence of bytesthat can be read and written. The current position can be queried and modified. The channel also PRovides access to the current size of the entity to which the channel is connected. The size increases when bytes are written beyond itscurrent size; the size decreases when it is truncated. The position and truncate methods which do not otherwise have a value to return are specified to return the channel upon which they are invoked. This allows method invocations to be chained. Implementations of this interface should specialize the return type so that method invocations on the implementation class can be chained.

而文件通道:FileChannel實現了該接口:

java.nio.channels.FileChannelA channel for reading, writing, mapping, and manipulating a file. A file channel is a SeekableByteChannel that is connected to a file. It has a current position within its file which can be both queried and modified. The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried. The size of the file increases when bytes are written beyond its current size; the size of the file decreases when it is truncated. The file may also have some associated metadata such as access permissions, content type, and last-modification time; this class does not define methods for metadata access. 

實例:

        Path filePath = Paths.get("D://error.log");        ByteBuffer buffer = ByteBuffer.allocate(1024);        buffer.put("111111".getBytes());        buffer.flip();        try {            FileChannel channel = FileChannel.open(filePath, StandardOpenOption.WRITE);            channel.position(80);    // 修改此處的內容為111111            channel.write(buffer);            channel.close();                        buffer = ByteBuffer.allocate(1024);            channel = FileChannel.open(filePath, StandardOpenOption.READ);            channel.position(100);    // 讀取此處的內容            channel.read(buffer, 10);            buffer.flip();            System.out.println(buffer);                        Charset charset = Charset.forName("utf-8");            CharsetDecoder decoder = charset.newDecoder();            CharBuffer charBuffer = decoder.decode(buffer);            System.out.println(charBuffer);                    } catch (IOException e) {            e.printStackTrace();        }

注意上面的例子中涉及到 ByteBuffer 到 CharBuffer 的轉換,也就是字節到字符的轉換,需要使用對應的編碼的解碼器進行解碼。


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 上饶县| 合作市| 桓仁| 仁化县| 云龙县| 溧阳市| 南丰县| 新邵县| 柏乡县| 岚皋县| 峨山| 新和县| 太白县| 隆子县| 米泉市| 清水河县| 江山市| 三原县| 兴宁市| 信宜市| 运城市| 永清县| 绥江县| 周宁县| 太保市| 兴化市| 渭源县| 仙桃市| 庆城县| 都兰县| 松原市| 墨脱县| 遵化市| 江津市| 东乡| 平塘县| 万年县| 临桂县| 瓦房店市| 健康| 麟游县|