java nio通过ByteBuffer输出文件信息

1.通过ByteBuffer的get()方法每次读取一个字节转换成char类型输出.

        fc = new FileInputStream("src/demo20/data.txt").getChannel();
        ByteBuffer buff = ByteBuffer.allocate(BSIZE);
        buff = ByteBuffer.allocateDirect(BSIZE);
        fc.read(buff);
        buff.flip();
        while (buff.hasRemaining()) {
            System.out.print((char)buff.get());
        }

2.使用系统字符集进行解码

FileChannel fc = new FileOutputStream("src/demo20/data2.txt").getChannel();
        fc.write(ByteBuffer.wrap("Some text".getBytes()));
buff.rewind();
String encoding = System.getProperty("file.encoding");//获取系统字符集
System.out.println("Decoded using "+ encoding + ":" + Charset.forName(encoding).decode(buff));//Decoded using GBK:Some text

System.getProperty可以获取系统字符集,可以用产生系统字符集的CharSet对象,来进行解码操作.

3.写入时进行编码

fc = new FileOutputStream("src/demo20/data2.txt").getChannel();
        fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
        fc.close();
        fc = new FileInputStream("src/demo20/data2.txt").getChannel();
        buff.clear();
        fc.read(buff);
        buff.flip();
        System.out.println(buff.asCharBuffer());

ByteBuffer.wrap()方法将 ""UTF-16BE"编码的 byte 数组包装到缓冲区中,写入文件,转换成CharBuffer即可读取文件.

4.通过CharBuffer写入

fc = new FileOutputStream("src/demo20/data2.txt").getChannel();
        buff = ByteBuffer.allocate(24);
        buff.asCharBuffer().put("Some text");
        fc.write(buff);
        fc.close();
        fc = new FileInputStream("src/demo20/data2.txt").getChannel();
        buff.clear();
        fc.read(buff);
        buff.flip();
        System.out.println(buff.asCharBuffer());

buff分配了24个字节,能储存12个字符,写入Some text可以通过FileInputStream获取的通道读取.

原文地址:https://www.cnblogs.com/lishuaiqi/p/10597574.html

时间: 2024-07-31 06:34:13

java nio通过ByteBuffer输出文件信息的相关文章

Java NIO 之 ByteBuffer 测试用例

分享一下,关于ByteBuffer的一些非常基础的测试用例 package xingej.buffer.test001; import java.nio.ByteBuffer; //注意:1.原生JAVA NIO的ByteBuffer的缓冲区是不能添加字符串的,其实,从名字也可以看出来,是Byte + Buffer =>ByteBuffer //也就是说,ByteBuffer是针对字节的缓存区 public class ByteBufferTest {     public static voi

Java NIO FileVisitor 高效删除文件

在公司项目中,由于做个二维码扫码平台项目,预计每天产生的二维码图片达到十几G,所以要做个定时清理任务来定时清理图片,根据不同场景保留图片,规则是:1.二维码统一登录图片几个小时有效   2.电子名片二维码前几天有效,这些参数都是可配置的. 刚开始时,直接用Io 文件操作,递归删除文件,根据场景判断删除逻辑.但是,压力测试后发现,删除十几G文件要20分钟,直接测试不过.原因分析:在所有的5级目录下,要遍历匹配条件的文件,将不符合文件的删除,删除又用了递归,所有一下有三层循环,这样直接遍历数据太大,

java nio读取和写入文件

读取 package com.test; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.text.MessageFormat; public class Test

Java NIO 利用通道完成文件复制(MappedByteBuffer)

相关学习网址: import java.io.IOException;import java.nio.MappedByteBuffer;import java.nio.channels.FileChannel;import java.nio.channels.FileChannel.MapMode;import java.nio.file.Paths;import java.nio.file.StandardOpenOption; import org.junit.Test; @Testpubl

Java NIO 之 ByteBuffer

抽象类ByteBuffer ByteBuffer的继承关系 父类 子类 目前,大概有5个实现类 ByteBuffer 底层,主要依赖? 顾名思义,从ByteBuffer的名称来看,这个缓冲区针对的是字节类型的缓冲区, 从源码中,也可以查询到, 就是字节数组 ByteBuffer的主要api 我这里仅仅分享一下,get,put,因为这两个方法用到的最多了. put 操作 目前有5个实现类,我们可以依次查看,到底如何实现的 DirectByteBuffer.java DirectByteBuffer

java nio 快速read大文件

If you want to make your first example faster FileChannel inChannel = new FileInputStream(fileName).getChannel(); ByteBuffer buffer = ByteBuffer.allocateDirect(CAPACITY); while(inChannel.read(buffer) > 0) buffer.clear(); // do something with the data

Java NIO 应用 -- 使用内存映射文件实现进程间通信

一看到 Java NIO 的内存映射文件(MappedByteBuffer),让我立即就联想到 Windows 系统的内存映射文件.Windows 系统的内存映射文件能用来在多个进程间共享数据,即进程间的共享内存,是通过把同一块内存区域映射到不同进程的地址空间中,从而达到共享内存. Java NIO 的内存映射文件和 Windows 系统下的一样,都能把物理文件的内容映射到内存中,那么 MappedByteBuffer 是否能用来在不同 Java 进程(JVM) 间共享数据呢?答案是肯定的,这样

将输出文件从分布式文件系统拷贝到本地文件系统查看

输入命令可查看在分布式文件系统下的输出文件信息: 也可将输出文件从分布式文件系统拷贝到本地文件系统查看:

Java NIO 进程间通信

转自:http://blog.csdn.net/lingzhm/article/details/45026119 传统的进程间通信的方式有大致如下几种: (1)   管道(PIPE) (2)   命名管道(FIFO) (3)   信号量(Semphore) (4)   消息队列(MessageQueue) (5)   共享内存(SharedMemory) (6)   Socket Java如何支持进程间通信.我们把Java进程理解为JVM进程.很明显,传统的这些大部分技术是无法被我们的应用程序利