集册 Java实例教程 使用旧的WritableByteChannel接口写入文件

使用旧的WritableByteChannel接口写入文件

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

465
使用旧的WritableByteChannel接口写入文件
/**来 自 nowjava.com**/

import java.io.IOException;

import java.nio.ByteBuffer;

import java.nio.channels.WritableByteChannel;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.StandardOpenOption;

import java.util.EnumSet;


public class Main {

  public static void main(String[] args) {

    Path path = Paths.get("C:/folder1/folder0/folder8", "story.txt");

    // write a file using WritableByteChannel

    try (WritableByteChannel writableByteChannel = Files.newByteChannel(path,

        EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.APPEND))) {

      ByteBuffer buffer = ByteBuffer.wrap("test test!".getBytes());

      int write = writableByteChannel.write(buffer);

      System.o
展开阅读全文