The Class java.io.File

The class java.io.File should really be called “Filename” since most of its methods are concerned with querying and adjusting filename, and pathname information, not the contents of a file. File can't actually do any I/O. Directory, filename and pathname information is often called “metadata,” meaning “data about data.” Methods of java.io.File allow you to access metadata to:

  • Return a File object from a String containing a pathname

  • Test whether a file exists, is readable/writable, or is a directory

  • Say how many bytes are in the file and when it was last modified

  • Delete the file, or create directory paths

  • Get various forms of the file pathname.

Here are all the important public members of java.io.File. Method names are in bold for visibility.

Public members of java.io.File

public class File implements Serializable, Comparable {
    public static final char separatorChar;
    public static final String separator;
    public static final char pathSeparatorChar;
    public static final String pathSeparator;

// constructors:
    public File(String path);
    public File(String directory,String file);
    public File(File directory,String file);

// about the file:
    public String getName();
    public String getParent();
    public File getParentFile();
    public String getPath();
    public String getAbsolutePath();
    public File getAbsoluteFile();
    public String getCanonicalPath() throws IOException;
    public File getCanonicalFile() throws IOException;

    public boolean canRead();
    public boolean canWrite();
    public boolean exists();
    public boolean isAbsolute();
    public boolean isDirectory();
    public boolean isFile();
    public boolean isHidden();
    public long lastModified();
    public long length();

// about the directory
    public String[]  list();
    public String[] list(FilenameFilter);
    public File[] listFiles();
    public File[] listFiles(FilenameFilter);
    public File[] listFiles(FileFilter);
    public static File[] listRoots();
    public boolean mkdir();
    public boolean mkdirs();
// using temporary files
    public boolean createNewFile() throws IOException;
    public boolean delete();
    public void deleteOnExit();
    public static File createTempFile(String, String) throws IOException;
    public static File createTempFile(String, String, File) throws IOException;

// miscellaneous:
    public boolean renameTo(File);
    public boolean setLastModified(long);
    public boolean setReadOnly();
    public int compareTo(File);
    public int compareTo(Object);
    public boolean equals(Object);
    public int hashCode();
    public String toString();
    public URL toURL() throws java.net.MalformedURLException;
}

You create a File object by giving strings for the directory and filename. The file doesn't actually have to exist when you instantiate the File object, and you can go on to create it using createNewFile(). You only bother to instantiate a File object if one of the operations listed previously is of interest to you. If you just want to do some I/O, then keep reading—that information is coming soon. Most of the method names in File give a clear indication of what they do. Here are the details on some of the less obvious ones.

public int compareTo(File);

This method compares the pathnames of two files for equality or otherwise. Filename comparisons are platform dependent, as Microsoft Windows does not distinguish letter case in filenames. A straight alphabetic comparison would give the wrong result on Windows, but the compareTo()method ensures the correct ordering for the platform.

public static File createTempFile(String prefix, String suffix) throws IOException;

This routine creates a temporary file in the default temporary directory. A unique filename will be generated for you, and it will have the prefix and suffix that you provide. This lets you give all temporary files created by particular program names that start and end with specified letters. For instance, you could specify that all temporary files created by your mail program should use a name that starts with “mail-” and ends with “.tmp.”, like mail-editchanges.tmp

There is another form of this method that takes a third parameter, a File object, to specify the directory.

public boolean createNewFile() throws IOException;
public void deleteOnExit();

These two routines could be used together to provide a simple file-locking protocol, giving exclusive access to some other file or resource. The createNewFile() method either atomically creates a new file, or returns “false” if the file already exists. “Atomically create” means that the check for the existence of the file, and the creation of the file if it does not exist, form a single operation. If there are several copies of your program running all making the same call at the same time, only one of them will succeed in creating the file. JDK 1.4 introduced the java.nio package, which provides more direct support for file locking.

public boolean mkdir();
public boolean mkdirs();

The first method creates just that directory. The second creates that directory plus any non-existent parent directories as needed.

public String[]  list();
public File[] listFiles();

The first method returns an array of strings representing the files and directories in the directory the method is invoked on. The second method returns the same information, but as File objects, not strings.

public static File[] listRoots();

This method lists all the available filesystems on this system. On Windows systems, the array will hold File objects for “A:”, “C:”, “D:”, and so on, allowing the programmer to learn what active drives there are. On Unix, the root drive is just “/” the root file system. On Windows, File objects for the root directories of the local and mapped network drives will be returned. Windows UNC pathnames (Universal Naming Convention pathnames that start with “//”) indicate a non-local file and are not returned by this method.

FileDescriptor

The basic operating system object used to manipulate files is called a file descriptor, but you're not expected to create them or work with them much in Java.

Just to give you a brief overview, we will mention what descriptors are, and then move on to some tips on portable I/O. A file descriptor is a non-negative integer, such as0,1,2,3... etc.. It is used by the native I/O system calls to index a control structure containing data about each open file or socket. Each process has its own table of file descriptors, with each entry pointing to an entry in a system-wide file descriptor table. The size of the process descriptor table places a limit on the number of files or sockets that a process can have open simultaneously. A typical size is 256 or more file descriptors.

Java applications should not create their own file descriptors. The FileInputStream and FileOutputStream classes have methods that get the file descriptor for a file that you have open, and that open the file for a descriptor that you have. The class java.io.FileDescriptor is used when the operating system needs a file descriptor, perhaps for a JNI call, or as part of the run-time library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.166.31