Obtaining Disk Space Information
amount of partition free space is important to installers and other applications. Until Java 6 arrived,
the only portable way to accomplish this task was to guess by creating files of different sizes.
Java 6 added to the File class long getFreeSpace(), long getTotalSpace(), and long
getUsableSpace() methods that return space information about the partition described by the File
instance’s abstract pathname. Android also supports these methods:
long getFreeSpace() returns the number of unallocated bytes in the partition
identified by this File object’s abstract pathname; it returns zero when the
abstract pathname doesn’t name a partition.
long getTotalSpace() returns the size (in bytes) of the partition identified by this
File object’s abstract pathname; it returns zero when the abstract pathname
doesn’t name a partition.
long getUsableSpace() returns the number of bytes available to the current
virtual machine on the partition identified by this File object’s abstract
pathname; it returns zero when the abstract pathname doesn’t name a partition.
Although getFreeSpace() and getUsableSpace() appear to be equivalent, they differ in the following
respect: unlike getFreeSpace(), getUsableSpace() checks for write permissions and other platformrestrictions, resulting in a more accurate estimate.
Note The getFreeSpace() and getUsableSpace() methods return a hint (not a guarantee) that a
Java application can use all (or most) of the unallocated or available bytes. These values are a hint because a
program running outside the virtual machine can allocate partition space, resulting in actual unallocated and
available values being lower than the values returned by these methods.
注意
Listing 11-4 presents an application that demonstrates these methods. After obtaining an array of all
available filesystem roots, this application obtains and outputs the free, total, and usable space for
each partition identified by the array.
Listing 11-4. Outputting the Free, Usable, and Total Space on All Partitions
import java.io.File;
public class PartitionSpace
{
public static void main(String[] args)
{
File[] roots = File.listRoots();
for (File root: roots)
{
System.out.println("Partition: " + root);
System.out.println("Free space on this partition = " +
root.getFreeSpace());
System.out.println("Usable space on this partition = " +
root.getUsableSpace());
System.out.println("Total space on this partition = " +
root.getTotalSpace());
System.out.println("***");
}
}
}
Compile Listing 11-4 (javac PartitionSpace.java) and run the application (java PartitionSpace).
When run on my Windows 7 machine with a hard drive designated as C:, a DVD drive designated as
D:, a USB drive designated as E:, and an extra drive designated as F:, I observe the following output
(usually with different free/usable space amounts on C: and E:):
Partition: C:\
Free space on this partition = 374407311360
Usable space on this partition = 374407311360
Total space on this partition = 499808989184
***
Partition: D:\
Free space on this partition = 0
Usable space on this partition = 0
Total space on this partition = 0
***
Listing Directories
列举目录
File declares five methods that return the names of files and directories located in the directory
identified by a File object’s abstract pathname. Table 11-3 describes these methods.
Table 11-3. File Methods for Obtaining Directory Content
Method Description
String[] list() Returns a potentially empty array of strings naming the files and directories
in the directory denoted by this File object’s abstract pathname. If the
pathname doesn’t denote a directory, or if an I/O error occurs, this method
returns null. Otherwise, it returns an array of strings, one string for each file
or directory in the directory.
Names denoting the directory itself and the directory’s parent directory are
not included in the result. Each string is a filename rather than a complete
path. Also, there is no guarantee that the name strings in the resulting array
will appear in alphabetical or any other order.
String[] list(FilenameFilter filter)
A convenience method for calling list() and returning only those Strings
that satisfy filter.
File[] listFiles() A convenience method for calling list(), converting its array of Strings to
an array of Files, and returning the Files array.
File[] listFiles(FileFilter filter)
A convenience method for calling list(), converting its array of Strings to
an array of Files, but only for those Strings that satisfy filter, and
returning the Files array.
File[] listFiles(FilenameFilter filter)
A convenience method for calling list(), converting its array of Strings to
an array of Files, but only for those Strings that satisfy filter, and
returning the Files array.
The overloaded list() methods return arrays of Strings denoting file and directory names. The
second method lets you return only those names of interest (such as only those names that end with
extension .txt) via a java.io.FilenameFilter-based filter object.
The FilenameFilter interface declares a single boolean accept(File dir, String name) method
that is called for each file/directory located in the directory identified by the File object’s pathname:
dir identifies the parent portion of the pathname (the directory path).
name identifies the final directory name or the filename portion of the pathname.
The accept() method uses the arguments passed to these parameters to determine whether or not
the file or directory satisfies its criteria for what is acceptable. It returns true when the file/directory
name should be included in the returned array; otherwise, this method returns false.
Listing 11-5 presents a Dir(ectory) application that uses list(FilenameFilter) to obtain only those
names that end with a specific extension.
Listing 11-5. Listing Specific Names
import java.io.File;
import java.io.FilenameFilter;
public class Dir
{
public static void main(final String[] args)
{
if (args.length != 2)
{
System.err.println("usage: java Dir dirpath ext");
return;
}
File file = new File(args[0]);
FilenameFilter fnf = new FilenameFilter()
{
@Override
public boolean accept(File dir, String name)
{
return name.endsWith(args[1]);
}
};
String[] names = file.list(fnf);
for (String name: names)
System.out.println(name);
}
}
When I, for example, specify java Dir c:\windows exe on my Windows 7 platform, Dir outputs only
those \windows directory filenames that have the .exe extension:
bfsvc.exe
explorer.exe
fveupdate.exe
HelpPane.exe
hh.exe
notepad.exe
regedit.exe
splwow64.exe
twunk_16.exe
twunk_32.exe
winhlp32.exe
write.exe
The overloaded listFiles() methods return arrays of Files. For the most part, they’re symmetrical
with their list() counterparts. However, listFiles(FileFilter) introduces an asymmetry.
The java.io.FileFilter interface declares a single boolean accept(String pathname) method that
is called for each file/directory located in the directory identified by the File object’s pathname; the
argument passed to pathname identifies the complete path of the file or directory.
The accept() method uses this argument to determine whether or not the file or directory satisfies its
criteria for what is acceptable. It returns true when the file/directory name should be included in the
returned array; otherwise, this method returns false.
Note Because each interface’s accept() method accomplishes the same task, you might be
wondering which interface to use. If you prefer a path broken into its directory and name components, use
FilenameFilter. However, if you prefer a complete pathname, use FileFilter; you can always call
getParent() and getName() to get these components.
注意 因为每个接口的accept()方法完成相同的任务,您可能不知道要使用哪个接口。如果你喜欢一个路径分为目录和名称组件,使用FilenameFilter。
然而,如果你喜欢一个完整的路径名,使用FileFilter;你可以随时调用getParent()和getName()来获得这些组件。