Java Reference
In-Depth Information
}
}
}
}
GetFileStores.java
On my system I get the following output:
Store: Windows7 NTFS Capacity: 231gb
Unallocated: 139gb
Store: New Volume NTFS Capacity: 931gb
Unallocated: 897gb
Store: share NTFS Capacity: 917gb
Unallocated: 822gb
How It Works
The getDefault() method in the FileSystems class returns the default file system as an object of type
FileSystem . Calling the getFileStores() method for filesystem returns the Iterable<FileStore>
collection of FileStore objects within the file system. The collection-based for loop iterates over each
of the FileStore objects in the collection and outputs the details. The capacity and available storage
values are divided by gigabyte to express the results in gigabytes. Because some of the FileStore class
methods can throw exceptions, the output statement is in a try block.
WORKING WITH PATH OBJECTS
A java.nio.file.Path object encapsulates a system-dependent file path to a file or directory. Path is an
interface type so you cannot create Path objects directly. You call the getPath() method for a FileSystem
object to obtain a Path object encapsulating a given file or directory path:
Path path = fileSystem.getPath(
"C: /Program Files (x86)/Java /jdk1.7.0/src/java/nio/file");
The argument is a string specifying the path. On my system, this happens to be the path to the directory
file , which contains the source files for classes in the java.nio.file package. The getPath() method
throws an exception of type java.nio.file.InvalidPathException if the path string is not a valid path
for your storage system. This is an unchecked exception so you are not obliged to catch it.
You can also specify the path as a series of strings:
Path path = fileSystem.getPath("C: /Program Files (x86)",
"Java ","jdk1.7.0/src/java/nio/file");
The three arguments here are joined to form the path string that is the same as the code fragment above.
Note that a path separator is inserted between strings when you specify more than one argument. In general
you can use as many strings as you like to specify the path.
Search WWH ::




Custom Search