Java nio2

Introduction to Java NIO2

This post is aiming to provide a high-level overview of the Java NIO2 API features. Java7 introduced a rich set of features to operators on the Files using Java NIO2 package.

In this post, we will learn about some of the basic features introduced by Java Nio2. The java.nio.file package provides a comprehensive support to work on the file IO. Files class is one of the main utility class provided by NIO package which provides a rich set of functionality to work on the underlying filesystem with help of Path class.

 

1.  Basics

In order to use Files or Path class, all we need to import java.io.file.* in our program, all major classes are defined within the java.io.file package.

Please read Java NIO Path to get understanding how Path class work in the new Java NIO2 package.

 

2. Checking a File or Directory

Java NIO package’s Files and Path API can be used to easily determine if a given path exists or not by utilizing exists and notExists method provided by Files class.

public static void checkIfFileDirExist(final Path location){
    System.out.println(Files.exists(location));
}

public static void checkIfFileDirNotExist(final Path location){
    System.out.println(Files.notExists(location));
}

 

Please be aware Files.notExists work for both Files and Directories, so refer to both files and directory when I am talking about files in this post.  

 

3. Checking  File Accessibility

Java NIO2 package also provides a way to check if given programme can access a file needed or do we have required permission to perform given file operation. Files utility class provide following method to check accessibility.

  1. isReadable(Path)
  2. isWritable(Path)
  3. isRegularFile (Path)
  4. isExecutable (Path)
   public static void checkIfReadable(final Path location)
    {
        System.out.println(Files.isReadable(location));
    }

   public static void checkIfWritable(final Path location)
    {
       System.out.println(Files.isWritable(location));
    }

A regular file is a file which is not special, in case file contains any symbolic links to similar properties, this will not be treated a regular file.

If you want to check if both Path objects are same or pointing to the same location, we can use isSameFile(Path path1, Path path2).

 

4. Creating Files

Files class provide an easy way to create the file using NIO. Please read How to write to file in Java for more details.

 

5. Creating Directory

Read How to create directory in Java to learn various options available under Java 7 NIO.

 

6.  Delete a directory in Java

You can delete files, directories or links, please read Deleting Directory in Java to understand how we can use walkFileTree to recursively delete directories and files using Files NIO.

 

7.  Move Directory / Files  in Java

Java NIO provides an easy way to move files or directories using API’s move method. You can also copy files and directories recursively using the fileWalker method. Please read  How to Move a File in Java.

 

8. Copying a File or Directory

Copy a file or directory in Java is a common operation. Java NIO Files class provide a convenient copy method to copy file or directory. If you want to copy files from one directory to another in Java including subdirectories, you have to use Files.walkFileTree from NIO Java7 approach to recursively copy directory and all files to another location.

Please read Copy a File or Directory in Java to see how to use FileVisitor to recursively copy files and directories. 

 

In this post, we saw of the core features introduced as part of the Java NIO2 introduced with JDK7 along with some of the important operation being supported by NIO2 API.

 

All the code of this article is available Over on Github. This is a Maven-based project.

Comments are closed.