将字符串写入到文件中NIO FileChannel

本文详细介绍使用Java NIO进行文件读写的具体方法,包括文本内容写入、图片文件拷贝及通过FileChannel进行高效数据传输的过程。文章还探讨了ByteBuffer在数据处理中的关键作用,如flip方法的必要性。

// 通过Java NIO将文本内容写入文本文档中
FileOutputStream outputStream = new FileOutputStream(new File("data.txt"));
FileChannel channel = outputStream.getChannel();

opStream = new ByteSeq();  // 初始化字节型序列
ByteBuffer byteBuffer = ByteBuffer.allocate(1024); 
String string = "java nio";
byteBuffer.put(string.getBytes());
byteBuffer.flip(); // 调用channel的write方法前必须调用过buffer的flip方法,否则无法正确写入内容
channel.write(byteBuffer);
channel.close();
outputStream.close();

// 查看字节数组的内容
for(int i = 0;i < bytes;i++){
    System.out.println(Integer.toHexString(i));
}

// 通过Java NIO、FileChannel实现图片文件的拷贝
FileInputStream inputStream = new FileInputStream("home-hero.jpg"); // 标准的字节流 
FileChannel ipStreamChannel = inputStream.getChannel(); // 获取文件通道实例,通过其读写文件

FileOutputStream outputStream = new FileOutputStream("home-hero_new.jpg");
FileChannel opStreamChannel = outputStream.getChannel();

ByteBuffer ipByteBuffer = ByteBuffer.allocateDirect(bufferSize);
int bytes = 0;

Long startTime = System.nanoTime();
while((bytes=ipStreamChannel.read(ipByteBuffer)) != -1){
    
    ipByteBuffer.flip(); // 调用channel的write方法前必须调用过buffer的flip方法,否则无法正确写入内容
    opStreamChannel.write(ipByteBuffer);
    ipByteBuffer.clear(); // for the next read
    
    /*for(int i = 0;i < bytes;i++){
        System.out.println(Integer.toHexString(i));
    }*/
}

Long elapsedTime = System.nanoTime() - startTime;
System.out.println("消耗的时间是:" + elapsedTime/1000000000.0 +"秒");

ipStreamChannel.close();
inputStream.close();
opStreamChannel.close();
outputStream.close();


// 通过FileChannel的测试方法
IShijingServiceInterceptor iShijingInterceptor = new IShijingServiceInterceptor();
AuxParams aux = new AuxParams();
ByteSeq bs = new ByteSeq();

FileOutputStream outputStream = new FileOutputStream("E:\\imgtest\\home-hero_new.jpg");
FileChannel opStreamChannel = outputStream.getChannel();

int flag = iShijingInterceptor.getShijingTileByTileId("1111111", bs, aux);

if(flag == 0){
    ByteBuffer getByteBuffer = bs.getBuffer();
    getByteBuffer.flip(); // 调用channel的write方法前必须调用过buffer的flip方法,否则无法正确写入内容
    if(getByteBuffer.hasArray()){
        System.out.println("ok.");
    }else {
        System.out.println("not ok");
    }
    byte[]bss = getByteBuffer.array();
    System.out.println("length:"+bss.length);

    opStreamChannel.write(getByteBuffer);
    getByteBuffer.clear(); // for the next read
}
opStreamChannel.close();
outputStream.close();


//通过字节数组byte[]的测试方法
IShijingServiceInterceptor iShijingInterceptor = new IShijingServiceInterceptor();
AuxParams aux = new AuxParams();
ByteSeq bs = new ByteSeq();

FileOutputStream outputStream = new FileOutputStream("E:\\imgtest\\home-hero_new.jpg");
            
int flag = iShijingInterceptor.getShijingTileByTileId("1111111", bs, aux);
if(flag == 0){
    ByteBuffer getByteBuffer = bs.getBuffer();
    getByteBuffer.flip(); // 调用channel的write方法前必须调用过buffer的flip方法,否则无法正确写入内容
    if(getByteBuffer.hasArray()){
        System.out.println("ok.");
    }else {
        System.out.println("not ok");
    }
    byte[]bss = getByteBuffer.array();
    outputStream.write(bss, 0, bss.length);
    getByteBuffer.clear(); // for the next read
}
outputStream.close();

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

LuckyJiang.2021

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值