App下載
首頁javafilechannelJava I/O - 如何從FileOutputStream獲取FileChannel

Java I/O - 如何從FileOutputStream獲取FileChannel

我們想知道如何從FileOutputStream獲取FileChannel。
   

// Getting channels from streams
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class MainClass {
  private static final int BSIZE = 1024;

  public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text ".getBytes()));
    fc.close();
  }
}