Java Reference
In-Depth Information
Java nio2 file input and output
Recall from the introduction that Java 7 introduced the “NIO2” API to offer a new file system API.
Existing legacy file I/O methods remain supported, so that many code samples, books, tutorials, and
real-life code still apply “legacy I/O” API features.
Since newer means better in this case, we will start by applying the NIO2 API toward working with
files. The central player in NIO2 is the Path type and its little helper class Paths .
the path Interface
On your computer, files are stored in a way so that they can be easily retrieved and accessed later.
On most file systems, files are stored in a hierarchical structure, meaning as a tree. The top of a tree
is called a root node (e.g., C:\ ) under which a hierarchy of folders can be found. Each folder can
contain other folders or files.
This implies that all files on your computer can be identified by a unique path of traversal through the
tree. For example, on Windows, the “C:\projects\java book\structure outline.txt” path refers
to a text file that lives in the folder named java book , which in turn lives in the folder projects .
Note On Linux, Unix, and BSD, paths use a different separator character,
namely / instead of \ . When programming in Java on Windows, you can also
use / instead of \ . If you want to retrieve the separator in a programmatic
manner, you can call the following method, which returns a string:
FileSystems.getDefault().getSeparator();
The example we provided illustrated a so-called “absolute” path, meaning that the path started
from the root element in the file system hierarchy ( C:\ ) and worked its way down from there. A
path can also be “relative.” Relative paths need to be combined with other paths to access a file. For
example, when the current path is “C:\projects\" , the relative path “java book\structure out-
line.txt” would lead you to the file you had before. The relative path overview.txt would resolve
to “C:\projects\overview.txt” and the relative path "..\movies\vacation.avi” would resolve
to “C:\movies\vacation.avi” . Note the use of ".." , which traverses one level up the directory
tree. A single dot ( . ) in a path refers to the current directory.
Finally, on some systems, file systems can also support links, apart from files and folders. A link is a
special file that serves as a reference to another file. Operations on these links are automatically redi-
rected to the target location of the link. You don't need to concern yourself further regarding this
aspect, as they are very uncommon on Windows systems and you won't use them for most purposes.
In Java, the Path type, found under the java.nio.file package, represents a path in the file
system. Path objects contain the filename and directory list used to build the path, and can be used
to examine and work with files. Creating a path is done by using the get method on the Paths class,
also under java.nio.file , like so:
Path myPath = Paths.get("C:\\projects\\outline.txt");
 
Search WWH ::




Custom Search