Recipe 11.1 Getting File Information


Problem

You need to know all you can about a given file on disk.

Solution

Use a java.io.File object.

Discussion

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 a File 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 11-1 lists some of the informational methods.

Table 11-1. java.io.File methods

Return type

Method name

Meaning

boolean

exists( )

True if something of that name exists

String

getCanonicalPath( )

Full name

String

getName( )

Relative filename

String

getParent( )

Parent directory

boolean

canRead( )

True if file is readable

boolean

canWrite( )

True if file is writable

long

lastModified( )

File modification time

long

length( )

File size

boolean

isFile( )

True if it's a file

True if it's a directory (note: it might be neither)

boolean

isDirectory( )


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: FileStatus 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:\javasrc\dir_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 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 $

A typical Unix system has no autoexec.bat file. And Unix filenames (like those on a Mac) can consist of upper- and lowercase characters: what you type is what you get.



Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net