Ruang penulis
← Kembali ke jurnal
Core API

How do I write data into ByteBuffer using put method?

The snippet below show you how to write some bytes into the java.nio.ByteBuffer object through a call to the put() method. package org.kodejava.io; import java.nio.ByteBuffer; public class BufferPut { public static void main(String[] args) { ByteBuffer buffer = ByteBuffer.allocate(32); buffer.put((byte) 65); buffer.put((byte) 66); buffer.put((byte) 67); buffer.put((byte) 68); buffer.put((byte) 69); // Writes a sequence of bytes […]

DWayan · 22 Feb 2010 · 1 menit baca

Artikel oleh Wayan · Sumber: Kode Java ↗

Terjemahan artikel ini belum tersedia. Isi asli ditampilkan.

The snippet below show you how to write some bytes into the java.nio.ByteBuffer object through a call to the put() method.

package org.kodejava.io;

import java.nio.ByteBuffer;

public class BufferPut {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(32);
        buffer.put((byte) 65);
        buffer.put((byte) 66);
        buffer.put((byte) 67);
        buffer.put((byte) 68);
        buffer.put((byte) 69);


        // Writes a sequence of bytes
        byte[] bytes = new byte[]{70, 71, 72, 73, 74};
        buffer.put(bytes);

        // Write to the beginning of the buffer
        buffer.put(0, (byte) 75);

        buffer.flip();

        while (buffer.hasRemaining()) {
            System.out.print((char) buffer.get());
        }
    }
}
← Jelajahi catatan lainnya