The File
class has a number of
“informational” methods. To use any of these, you must
construct a File
object containing the name of the
file it is to operate upon. It should be noted up front that creating
aFile
object has no effect on the permanent filesystem; it is
only an object in Java’s memory. You must call methods on the
File
object in order to change the filesystem; as
we’ll see, there are numerous “change” methods,
such as one for creating a new (but empty) file, one for renaming a
file, etc., as well as many informational methods. Table 10-1 lists some of the informational methods.
Table 10-1. java.io.File methods
Return type |
Method name |
Meaning |
---|---|---|
|
|
True if something of that name exists |
|
|
Full name |
|
|
Relative filename |
|
|
Parent directory |
|
|
True if file is readable |
|
|
True if file is writable |
|
|
File modification time |
|
|
File size |
|
|
True if it’s a file |
|
|
True if it’s a directory (Note: it might be neither) |
You can’t change the name stored in a File
object; you simply create a new File
object each
time you need to refer to a different file.
import java.io.*; import java.util.*; /** * Report on a file's status in Java */ public class FileStatus { public static void main(String[] argv) throws IOException { // Ensure that a filename (or something) was given in argv[0] if (argv.length == 0) { System.err.println("Usage: Status filename"); System.exit(1); } for (int i = 0; i< argv.length; i++) { status(argv[i]); } } public static void status(String fileName) throws IOException { System.out.println("---" + fileName + "---"); // Construct a File object for the given file. File f = new File(fileName); // See if it actually exists if (!f.exists( )) { System.out.println("file not found"); System.out.println( ); // Blank line return; } // Print full name System.out.println("Canonical name " + f.getCanonicalPath( )); // Print parent directory if possible String p = f.getParent( ); if (p != null) { System.out.println("Parent directory: " + p); } // Check if the file is readable if (f.canRead( )) { System.out.println("File is readable."); } // Check if the file is writable if (f.canWrite( )) { System.out.println("File is writable."); } // Report on the modification time. Date d = new Date( ); d.setTime(f.lastModified( )); System.out.println("Last modified " + d); // See if file, directory, or other. If file, print size. if (f.isFile( )) { // Report on the file's size System.out.println("File size is " + f.length( ) + " bytes."); } else if (f.isDirectory( )) { System.out.println("It's a directory"); } else { System.out.println("I dunno! Neither a file nor a directory!"); } System.out.println( ); // blank line between entries } }
When run with the three arguments shown, it produces this output:
C:javasrcdir_file>java FileStatus / /tmp/id /autoexec.bat ---/--- Canonical name C: File is readable. File is writable. Last modified Thu Jan 01 00:00:00 GMT 1970 It's a directory ---/tmp/id--- file not found ---/autoexec.bat--- Canonical name C:AUTOEXEC.BAT Parent directory: File is readable. File is writable. Last modified Fri Sep 10 15:40:32 GMT 1999 File size is 308 bytes.
As you can see, the so-called " canonical name” not only includes a leading directory root of C: , but also has had the name converted to uppercase. You can tell I ran that on MS-Windows. On Unix, it behaves differently:
$ java FileStatus / /tmp/id /autoexec.bat ---/--- Canonical name / File is readable. Last modified October 4, 1999 6:29:14 AM PDT It's a directory ---/tmp/id--- Canonical name /tmp/id Parent directory: /tmp File is readable. File is writable. Last modified October 8, 1999 1:01:54 PM PDT File size is 0 bytes. ---/autoexec.bat--- file not found $
On a typical Unix system there is no
autoexec.bat
file. And Unix filenames (like
those on a Mac) can consists of upper- and lowercase
characters: what you type is what you get.
3.17.176.72