集册 Java实例教程 创建一个带有孔的文件

创建一个带有孔的文件

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

578
创建一个带有孔的文件

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

import java.nio.ByteBuffer;/* 来自 n o w j a v a . c o m*/

import java.nio.channels.FileChannel;


public class Main {

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

    File temp = File.createTempFile("holy", null);

    RandomAccessFile file = new RandomAccessFile(temp, "rw");

    FileChannel channel = file.getChannel();


    ByteBuffer byteBuffer = ByteBuffer.allocateDirect(100);

    /**
    来 自 N o  w  J a v a . c o m - 时  代  Java
    **/

    putData(0, byteBuffer, channel);

    putData(5000000, byteBuffer, channel);

    putData(50000, byteBuffer, channel);


    // Size will report the largest position written

    System.out.println("Wrote temp file '" + temp.getPath() + "', size="

        + channel.size());


    channel.close();

    file.close();

  }


  private static void putData(int position, ByteBuffer buffer,

      FileChannel channel) 
展开阅读全文