java.nio.file.Files methods with Examples

The java.nio.file package and its related package, java.nio.file.attribute, provide comprehensive

support for file I/O and for accessing the default file system. Though the API has many classes, you need to focus on only a few entry points.

The java.io package contains all the classes required for input and output operation.

We use Java NIO for the two main reasons:

1). Non-blocking IO operation:

Java NIO performs non-blocking IO operations. This means that it reads the data whichever is ready. For instance, a thread can ask a channel to read the data from a buffer and the thread can go for other work during that period and continue again from the previous point where it has left. In the meantime, the reading operation is complete which increases the overall efficiency.

2).Buffer oriented approach:

Java NIO’s buffer oriented approach allows us to move forth and back in the buffer as we need. The data is read into a buffer and cached there. Whenever the data is required, it is further processed from the buffer.

Is a Path?

file system stores and organizes files on some form of media,in one or more hard drives, in such a way that they can be easily retrieved. Most file systems in use today store the files in a tree (or hierarchical) structure. At the top of the tree is one (or more) root nodes. Under the root node, there are files and directories . Each directory can contain files and subdirectories, which in turn can contain files and subdirectories, and so on, potentially to an almost limitless depth.

What Is a Path?

The following figure shows a sample directory tree containing a single root node. Microsoft Windows supports multiple root nodes. Each root node maps to a volume, such as C:\ or D:\. The Solaris OS supports a single root node, which is denoted by the slash character, /.

A file is identified by its path through the file system, beginning from the root node.In linux we have file located as

/home/devuser/hello.txt

In Microsoft Windows, hello.txt is described by the following notation:

C:\users\balauser\hello.txt

The character used to separate the directory names (also called the delimiter or file separator) is specific to the file system: The Linux OS uses the forward slash (/), and Microsoft Windows uses the backslash slash (\).

Absolute Path

An absolute path always contains the root element and the complete directory list required to locate the file.

C:\users\balauser\hello.txt

Realtive path

A relative path needs to be combined with another path in order to access a file. For example, balauser\hello.txt is a relative path.

Creating a Path

A Path instance contains the information used to specify the location of a file or directory. A path contains more components. A root element or a file name might be included, but neither are required. A Path might consist of just a single directory or file name.

You can create path usinf the following mehtods of Paths class.

Path p1 = Paths.get("/hello/world"); Path p2 = Paths.get(args[0]); Path p3 = Paths.get(URI.create("file:///Users/bala/test.json"));

the shorthand notation is like

Path p4 = FileSystems.getDefault().getPath("/bala/test.json");

The following example creates /u/myapp/logs/appslog assuming your home directory is users\balauser, or C:\users\balauser\myapp\logs\app.log if you are on Windows.

Path p5 = Paths.get(System.getProperty("user.home"),"logs", "app.log");

getTotalSpace() method

The getTotalSpace() method of a FileStore class is used to return total size of the file store, in bytes.

public abstract long getTotalSpace() throws IOException

The example is:

import java.io.IOException; import java.nio.file.FileStore; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class GetTotalSpace { public static void main(String[] args) { Path path = Paths.get("/home/devuser/hello.txt"); try { FileStore fs = Files.getFileStore(path); System.out.println("The FileStore name is : " + fs.name()); System.out.println("The TotalSpace is : " + fs.getTotalSpace()); } catch(IOException e) { e.printStackTrace(); } } } output: The FileStore name is : /dev/sda1 The TotalSpace is : 10499674112

Post/Questions related to java.nio.file.Files methods with Examples

How to download a pdf file using java?
Create New File in Java
Read file to byte[] array in Java Java File Reader Examples
Java file writer Example
How to Download File from URL in Java
Java File Path, Absolute Path and Relative Path
Path toString() method in Java

In this article, we have seen java.nio.file.Files methods with Examples. All source code in the article can be found in the GitHub repository.