Trail: Essential Classes
Lesson: Basic I/O
File I/O (featuring NIO.2)
Home Page > Essential Classes > Basic I/O
File I/O (featuring NIO.2)

This section is being updated to reflect features and conventions of the upcoming release, JDK7. You can download the current JDK7 Snapshot from java.net. We've published this preliminary version so you can get the most current information now, and so you can tell us about errors, omissions, or improvements we can make to this tutorial.
The java.nio.file package and its related package, java.nio.file.attribute, provide comprehensive support for file I/O and for accessing the file system. Though the API has many classes, you need to focus on only a few entrypoints — you will see that this API is very intuitive and easy to use.

The tutorial starts by asking what is a Path? Then the Path class, the primary entrypoint for the package, is introduced. Methods in the Path class relating to syntactic operations are explained. The tutorial then moves on to Path methods that deal with file operations. First, some concepts common to many file operations are introduced. The tutorial then covers methods for checking, deleting, copying, and moving files.

The tutorial shows how metadata is managed, before moving on to file I/O and directory I/O. Random access files are explained and issues specific to symbolic and hard links are examined.

Next, some of the very powerful, but more advanced, topics are covered. First, the ability to recursively walk the file tree is demonstrated, followed by information on how to search for files using wild cards. Next, how to watch a directory for changes is explained and demonstrated. Then, methods that didn't fit elsewhere are given some attention.

Finally, if you have file I/O code written prior to JDK7, there is a map from the old API to the new API, as well as important information about the File.toPath method for those who would like to leverage the new API without re-writing existing code.

Previous page: Object Streams
Next page: What is a Path? (And Other File System Facts)