将ByteBuffer保存成文件

Posted passedbylove

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了将ByteBuffer保存成文件相关的知识,希望对你有一定的参考价值。

 String dest = "d:/download/" + name;

            Path path = Paths.get(dest).getParent().toAbsolutePath().normalize();

            if(!Files.exists(path))
            
                try 
                    Files.createDirectories(path);
                 catch (IOException e) 
                    e.printStackTrace();
                
            

            try (FileChannel fc = new FileOutputStream(dest).getChannel())

                ByteBuffer buffer = getResponseAttachment(url);
                fc.write(buffer);
             catch (IOException e) 
                e.printStackTrace();
            

 

import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main 
  public static void main(String[] argv) throws Exception 
    ByteBuffer bbuf = ByteBuffer.allocate(100);
    File file = new File("filename");

    boolean append = false;

    FileChannel wChannel = new FileOutputStream(file, append).getChannel();

    wChannel.write(bbuf);

    wChannel.close();
  
String dest = "d:/download/" + name;
            try (FileChannel fc = FileChannel.open(Paths.get(dest), StandardOpenOption.WRITE))
                ByteBuffer buffer = getResponseAttachment(url);
                fc.write(buffer);
             catch (IOException e) 
                e.printStackTrace();
            

 

以上是关于将ByteBuffer保存成文件的主要内容,如果未能解决你的问题,请参考以下文章

java nio通过ByteBuffer输出文件信息

Android 将数据从 ByteBuffer 存储到 Storage

如何在android的MediaCodec上下文中使用ByteBuffer

浅谈ByteBuffer转换成byte[]时遇到的问题

Java NIO中的Channel接口

如何在 Java 中将 ByteBuffer 转换为 FileInputStream?