集册 Java实例教程 在收集写入中收集许多缓冲区

在收集写入中收集许多缓冲区

欢马劈雪     最近更新时间:2020-01-02 10:19:05

462
在收集写入中收集许多缓冲区

import java.io.FileOutputStream;

import java.nio.ByteBuffer;

import java.nio.channels.GatheringByteChannel;

import java.util.LinkedList;/* from n o w j a v a . c o m - 时代Java*/

import java.util.List;


public class Main {

  private static final String fileName = "test.txt";


  public static void main(String[] argv) throws Exception {

    int reps = 10;


    FileOutputStream fos = new FileOutputStream(fileName);

    GatheringByteChannel gatherChannel = fos.getChannel();

    ByteBuffer[] bs = utterBS(reps);

    while (gatherChannel.write(bs) > 0) {

      // Loop until write( ) returns zero

    }

    System.out.println(fileName);


    fos.close();/** nowjava.com 提 供 **/

  }


  private static String newline = System.getProperty("line.separator");

  private static ByteBuffer[] utterBS(int howMany) throws Exception {

    List list = new LinkedList();

    for (int i = 0; i < howMany; i++) {

      list.add(pickRandom());

      list.add(pickRandom());

      list.add(pickRandom());

    }

    ByteBuffer[] bufs = new ByteBuffer[list.size()];

    list.toArray(bufs);

    return (bufs);

  }

  private static ByteBuffer pickRan
展开阅读全文