Home » Core Java » nio (page 5)

nio

Write to Channel with ByteBuffer

With this example we are going to demonstrate how to write data to a NIO Channel using a ByteBuffer in Java. In particular we are going to read data from a specific file in the file system and write them to a destination file. In short what we do is the following : To write data to a channel you ...

Read More »

Read from Channel with ByteBuffer

This is an example of how to read data from a NIO Channel using a ByteBuffer in Java. In particular we are going to read data from a specific file in the file system and print them on screen. In short what we do is the following : To read data from channel you should create a ReadableByteChannel. To do ...

Read More »

Create memory mapped file

This is an example of how to create a memory mapped file in Java. Doing reads and writes of data using Java NIO Channels implies that you should : Create a File object to encapsulate an actual file in the file system Create a random access file stream (read only, read-write). To do so you must first create a RandomAccessFile ...

Read More »

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 »