码迷,mamicode.com
首页 > 其他好文 > 详细

将ByteBuffer保存成文件

时间:2019-09-04 23:16:25      阅读:482      评论:0      收藏:0      [点我收藏+]

标签:puts   row   and   locate   bool   div   etc   path   nio   

 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保存成文件

标签:puts   row   and   locate   bool   div   etc   path   nio   

原文地址:https://www.cnblogs.com/passedbylove/p/11462112.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!