Home » nio » Page 4

Tag Archives: nio

Copying binary file with FileChannel

With this example we demonstrate how to copy files using FileChannels in Java. In particular we are going to read data from a specific file in the file system and write them to another file. In short what we do is the following : For the source file we create a FileChannel so as to be able to read data ...

Read More »

Create shared file lock on file

In this example we shall show you how to create a shared file lock in Java. Creating shared file locks using Java NIO Channels implies that you should : Create a File object to encapsulate an actual file in the file system that you want to lock to Create a random access file stream (read-write). To do so you must ...

Read More »

Create file lock on file

This is an example of how to create a file lock in Java. Creating file locks using Java NIO Channels implies that you should : Create a File object to encapsulate an actual file in the file system that you want to lock to Create a random access file stream (read-write). To do so you must first create a RandomAccessFile ...

Read More »

Write/Append to File with ByteBuffer

With this is example we are going to demonstrate how to write/append data to a file in Java using a ByteBuffer. Particularly we are going to read data from a source file and append them to the destination file. In short what we do is the following : Create a File object to encapsulate an actual file in the file ...

Read More »

Use ByteBuffer for non-byte Java types buffering

In this example we will demonstrate how to perform non-byte Java types buffering using a ByteBuffer in Java. In particular we are going to show you how to use a ByteBuffer to store the following Java types : char short int long float double     In all cases above we are going to use the standard API methods provided ...

Read More »

Use ByteBuffer to store Strings

This is an example of how to store Strings using a ByteBuffer in Java. In order to use a ByteBuffer to store Strings in Java we have to : Allocate a new ByteBuffer and set its size to a number large enough in order to avoid buffer to overflow when putting bytes to it Use the asCharBuffer() API method so ...

Read More »

Get byte from ByteBuffer

With this example we are going to demonstrate how to read bytes from a ByteBuffer. Additionally we will show you several of ByteBuffer‘s API methods in order to share some light on how to randomly ready data from it.               package com.javacodegeeks.snippets.core; import java.nio.ByteBuffer; public class GetByteFromByteBuffer { public static void main(String[] args) { ...

Read More »

Put byte into ByteBuffer

This is an example of how to put bytes into a ByteBuffer in Java. Additionally we will demonstrate several of ByteBuffer‘s API methods in order to share some light on how to randomly write data to it.                 package com.javacodegeeks.snippets.core; import java.nio.ByteBuffer; public class PutByteIntoByteBuffer { public static void main(String[] args) { // ...

Read More »

Create direct and non-direct ByteBuffer

In this example we are going to demonstrate several methods of creating a direct (memory-mapped) and non-direct ByteBuffer in Java. Given a direct byte buffer, the Java virtual machine will make a best effort to perform native I/O operations directly upon it. That is, it will attempt to avoid copying the buffer’s content to (or from) an intermediate buffer before ...

Read More »

Convert between ByteBuffer and byte array

With this example we are going to demonstrate how to convert between ByteBuffers and byte arrays. In short, to make a conversion between a ByteBuffer and a byte array you should: Create a byte array and wrap it into a ByteBuffer. The buffer’s capacity and limit will be the array’s length and its position will be zero. Retrieve the bytes ...

Read More »