Eric Bruno

Dr. Dobb's Bloggers

Java SE 7 - New File IO

August 29, 2011

Java SE 7 has many enhancements and new APIs. In a previous blog, I discussed the new Fork/Join framework. In this blog, I'll talk briefly about the new Java.NIO 2.0 APIs, and specifically about how much easier it is to work with files. To be clear, although NIO used to mean New IO, Oracle now says that it really stands for Non-blocking IO.

Java NIO 2.0 (sometimes referred to as NIO.2) provides comprehensive support for the filesystem. Besides enhancements to the way you create, open, read, and write to files, you can now create custom filesystems and file providers in Java as well. One example is the ZIP file provider, which comes with the API and treats an archive file as a filesystem in its own right. In this specific case, the ZIP filesystem makes it much easier to work with ZIP archive files for both creation and expansion. We'll examine Java SE 7 File System Providers in a future blog. For now, let's dive into the new File IO APIs.

Java 1.6 File IO: Ancient History

Prior to Java SE 7, File IO involved a fair amount of code, some of it nonintuitive and difficult to remember even for Java veterans. For instance, I would often find myself searching my older code and cutting/pasting into new projects. Here's an example of some of the tedious code required in Java SE 6 to open a file:

Or perhaps you know the format of the data, such as a file that contains a list of values of the primitive type double. To work with such data-formatted file content, you could use the DataInputStream class:

Lots of choices, lots of code. Java SE 7 does much to simplify and unify all of this. Let's look at some examples to contrast now.

Java 1.7 Files Class

First, get to know the Files class  new in Java SE 1.7. With it, you can easily create new files, create temporary files, create new (and temporary) directories, copy and move files various different ways, create symbolic links, open existing files, delete files, work with file attributes, read from files, and write to files. Just about everything you've ever wanted to do with a file can now be done through this one class. What I like about this approach is that it's easy to remember, and saves me from fumbling through the API docs or through past projects to find File IO code.

The Files class is your one entry point for most file operations, and all you need to do is type"'Files" in your IDE to see the list of API methods at your disposal. Awesome!

Want to open an existing file by path? Here it is:

Path path = FileSystems.getDefault().getPath(".", name);

Want to read all of the bytes in that file? No more looping, here it is:

In fact, the StandardOpenOption class in Java 1.7 contains all of the file and directory attributes you need to deal with file existence create, delete, overwrite, append, and other options. Again, having this all in one helper class helps a great deal.

But what about all that existing File IO code you have? Do you need to throw it all away and start over to use the new Files class? No way! The old and new File IO APIs can be used together:

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!