Join David Gassner for an in-depth discussion in this video Using the Path class, part of Advanced Java Programming.
In Java 7 there's a new system for working with directories and files. The classes that are a part of the system are part of a package called Java.nio, for new input-output. The core of this new system is a class called path that replaces the old file class. A path can represent either a directory or a file. I'll start in this video with a simple demonstration of using the path to point to a file. I'm working in pretty much an empty project that has a main class with an empty main method, and a simple text file called loremipsum.txt.
I'll start by creating a Path Object that points to that file. I'll declare a variable data typed as Path. I'll make sure to include the import for the Path class, which again, is a member of the package, Java.nio. I'll name the variable Path, and now instantiate it using a static method of a class called Paths, that's P-A-T-H-S, plural. This is also a member of the same package, and it has a static method called get.
When you call the get method, you can either pass in a URI object that's the URI class that's the part of the Java.net package, or you can pass in a simple string that points to the location or name of the file. And I'll type the name of my file "loremipsum.txt". Now, a path doesn't necessarily need to point to something that already exists, especially, if you're going to use it to create a new file. But I'm going to be showing you one particular capability of the Path class that does require in existing file.
So, now, let's explore what kind of information you can get from a Path Object. I'm going to be outputting a bunch of information that comes from this Path Object representing my file. First, let's take a look at the toString method. The toString method is implemented on all Java classes, and for the Path Object it gives you the name of the file as it was passed in when it was first created. Next, I'll call a method called getFileName.
The getFileName method we'll return pretty much of the same thing as toString. When you create a Path Object by only passing in the file name, the toString method and getFileName return the same thing. But let's say, for example, that I'd wanted to point to a txt file in a folder called temp, right after my root directory. I will change my get method to call "/temp/loremipsum.txt", then I'll run the code again, and I'll see that in fact these two methods are different.
The toString method returns what I passed in with adjustments for the back slash or forward slash depending on what operating system that I'm working on, whereas getFileName only returns the last part of the string that is the actual name of the file without its location. When your path is represented by both the file name, and some information about the location, it's said to have more than one name, and you can find out how many names there are by calling a method called getNameCount.
I'll once again output to the system, and I'll call path.getNameCount, and because I have one directory plus the file name that gives me a total name count of 2, and once I know, how many names I have, I can extract one of them by pointing to it by index. So, if I have a name count of 2, that means I have names at index 0 and index 1, just like in an array. So, let's say that I wanted to extract the first of the two names. That would be the method getName, and I'll pass in an index of 0, and that gives me a return value of temp, the first name.
If I change that 1, I'll get back the name of the file, loremipsum.txt. And if you only want the last name, you can combine the getName and getNameCount methods like this. I'll get rid of the console, so, I can see all this code I'm about to type. And I'll replace the literal one with path.getNameCount()-1. So, the name count is 2, but the highest possible index is 1 less than that. So, this would always return the actual name of the file.
And there's my result loremipsum.txt. Finally, I'll show you a method called toRealPath. I'll come back up here and change to the just the file name, and I'm doing that to make sure I'm pointing to a real existing file. The toRealPath method only operates correctly when you're dealing with an actual file or actual directory. If you try to point to a non-existing directory or file, it will throw an exception. I'll create a new Path Object named realPath, and I'll get its value by calling path.toRealPath, there's a class called LinkOption, and it's an enumerator class that currently has only one option, NOFOLLOW_LINKS.
So, that means that I'm not going to follow symbolic links, I'm only dealing with actual path names. Notice that this code generates a compiler error, telling me that I might get an IOException. So to fix that I'll go up to my main method, I'll add a throws clause, and I'll add IOException. I'll make sure to include the import statement for that class, I'll save, and that error is gone. And finally, I'll output the value of the RealPath as a string, I'll save and run again, and here is the result.
First, I'm outputting the value that I passed in as a string, then the file name, then the number of names, then the last part of the name, and finally the RealPath. And I'm working on Windows, so I get the full path starting with C:\, and so on. But if you're working on a Mac or on Linux, your output will start with forward slash and should also include the full path. You can do a lot more with the Path class. Now, I'll show you some of the tasks you can do in other videos of this chapter.
But you might want to take a look at the documentation. I'll click into the name of the class and press F1 on Windows, or go to Help, then Dynamic Help on Mac, and then click into the Javadoc for the Path class. From there, I'll click into the Method link, and here are all the different methods that you can use with a Path Object. You'll see that many of them return other instances of the Path class, and there is also a class called FileSystem which returns an object representing your entire FileSystem.
You'll also see a class reference here called WatchKey, and that's used to create watches on directories that you can react to in your applications. There's a lot to learn about the new Path class, but you should find that the Path class is a lot easier to use, and takes less code to use than the file class that's used with previous versions of Java.
- Installing Java on Windows and Mac OS X
- Installing Eclipse
- Using new features such as simplified generics
- Working with advanced class structures (member, local inner, etc.)
- Using the Reflection API
- Navigating inheritance trees
- Managing unordered and ordered sets
- Peeking and polling with queues
- Testing and error handling
- Managing files and directories
- Working with I/O streams
- Next steps with Java