Java Reference
In-Depth Information
Iterable<FileStore> stores = fileSystem.getFileStores();
The result is returned as an object of type Iterable<FileStore> . This is a generic type that you learn
about in Chapter 13; for now just take it for granted. The Iterable<> type is defined in the java.lang
package as an Iterable<FileStore> object represents a collection of FileStore objects that you can it-
erate over using the collection-based for loop that you read about in Chapter 3. A FileStore object has
methods that provide information about the storage that it represents. The more interesting ones are presen-
ted in Table 9-1 .
TABLE 9-1 : FileStore Methods
METHOD DESCRIPTION
name() Returns the name of the file store as type String .
type() Returns the type of the file store as type String .
getTotalSpace() Returns the total capacity of the file store in bytes as a value of type long . Throws an excep-
tion of type IOException if an error occurs.
getUnallocatedSpace() Returns the unallocated space available in the file store in bytes as a value of type long .
The last two methods in the table can throw an exception of type IOException if an I/O error occurs.
Let's try these in an example.
TRY IT OUT: Getting Information about File Stores
This program lists details of the file stores on a system:
import java.nio.file.FileStore;
import java.nio.file.FileSystems;
import java.nio.file.FileSystem;
import java.io.IOException;
public class GetFileStores {
public static void main(String[] args) {
FileSystem fileSystem = FileSystems.getDefault();
Iterable<FileStore> stores = fileSystem.getFileStores();
long gigabyte = 1_073_741_824L;
for(FileStore store:stores){
try {
System.out.format(
"\nStore: %-20s %-5s
Capacity: %5dgb
Unallocated: %6dgb",
store.name(),
store.type(),
store.getTotalSpace()/gigabyte,
store.getUnallocatedSpace()/gigabyte);
} catch(IOException e) {
e.printStackTrace();
 
 
Search WWH ::




Custom Search