Home » Core Java » nio (page 4)

nio

java.nio.channels.CompletionHandler Example

This article introduces the CompletionHandler and its basic usage. This interface is available since Java SE 7 as part of Java NIO 2 File API. This article’s example shows reading from a file using asynchronous file channel and using the completion handler to consume its result. The example in this article is compiled and run in Windows OS environment. Note ...

Read More »

java.nio.channels.FileChannel Example

This article introduces the FileChannel class and its basic usage. This class is available since Java 1.4 as part of Java NIO (New IO) File API. This article shows reading from and writing to file using file channels. The examples in this article are compiled and run in Windows OS environment. Note that Java SE 7 is required to run ...

Read More »

java.nio.file.WatchEvent Example

WatchEvent<T> is an interface defined in the java.nio.file package. The type parameter T is the type of the context object associated with the event. This interface has been in Java since Java SE 7 as part of NIO 2 File APIs. This is part of file change notification API, called the Watch Service API. Watch event in general represents an ...

Read More »

java.nio.channels.AsynchronousFileChannel Example

This article introduces the AsynchronousFileChannel class and its basic usage. This class is available since the Java SE 7 as part of Java NIO 2 File API. This article shows reading from and writing to file using asynchronous file channels. The examples in this article are compiled and run in Windows OS environment. Note that Java SE 7 is required ...

Read More »

java.nio.file.Path Example

This article introduces the Path interface and its basic usage. The Path interface is available in the Java SE 7 as part of Java NIO 2 File API. This article shows creating, getting information, converting and comparing paths.  The examples in this article are compiled and run in Windows OS environment. Note that Java SE 7 is required to run ...

Read More »

Java MappedByteBuffer Example

In this post, we are going to discuss about the class java.nio.MappedByteBuffer                   1. MappedByteBuffer Class There are two ways for reading a file, sequentially and randomly. Files that can be explored sequentially are known as sequential files. Files that permit random access to their contents are known as random access files ...

Read More »

4 Ways to Copy File in Java

Although Java offers a class that can handle file operations, that is java.io.File, it doesn’t have a copy method that will copy a file to another. The copying action is an important one, when your program has to handle many file related activities. Nevertheless, there are several ways you can perform a file copying operation in Java and we will ...

Read More »

Convert Between Character Set Encodings with CharBuffer

In this example we shall show you how to convert between character set encodings with a CharBuffer in Java. To achieve character set conversions in Java one should perform the following steps: Use the Charset class to get the Charset object for utilizing the specific character set Use the newDecoder() API method of the Charset object to construct a new ...

Read More »

Convert String to byte array UTF encoding

With this example we are going to demonstrate how to convert a String to byte array and vice-versa using the default character encoding in Java. In short, to perform the aforementioned conversion, we are going to use classes from the NIO package in Java so as to convert every character from the target String to its byte equivalent. Namely we ...

Read More »

Create Stream from FileChannel

This is an example of how to create input and output streams to read and write data from/to a 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 To read data from the file you should create a ...

Read More »