Java File Write via ByteBuffer writeStringContents(String contents, WritableByteChannel channel)

Here you can find the source of writeStringContents(String contents, WritableByteChannel channel)

Description

Writes String contents to channel and closes it.

License

Apache License

Declaration

public static void writeStringContents(String contents, WritableByteChannel channel) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.IOException;
import java.nio.ByteBuffer;

import java.nio.channels.WritableByteChannel;
import java.nio.charset.StandardCharsets;

public class Main {
    /** Writes String contents to channel and closes it. */
    public static void writeStringContents(String contents, WritableByteChannel channel) throws IOException {
        // TODO Checks if a supplier would be nice
        try {//from   w  w w  . j a v a2s.co m
            ByteBuffer buffer = ByteBuffer.wrap(contents.getBytes(StandardCharsets.UTF_8));
            channel.write(buffer);
        } finally {
            channel.close();
        }
    }
}

Related

  1. writeLong(OutputStream os, Long d)
  2. writeLong(RandomAccessFile raFile, long num)
  3. writeObject(Serializable serializable)
  4. writeShort(int i, DataOutput out)
  5. writeString(ByteArrayOutputStream bout, String val)
  6. writeStringData(DataOutputStream mDataOutputStream, String token)
  7. writeStringToSocketChannel(String text, SocketChannel sc)
  8. writeToFile(String path, ByteOrder byteOrder)
  9. writeUtf8(DataOutput out, String string)