img
Here, directoryPath is the path name of the file, filename is the name of the file or subdirectory,
dirObj is a File object that specifies a directory, and uriObj is a URI object that describes
a file.
The following example creates three files: f1, f2, and f3. The first File object is constructed
with a directory path as the only argument. The second includes two arguments--the path
and the filename. The third includes the file path assigned to f1 and a filename; f3 refers to the
same file as f2.
File f1 = new File("/");
File f2 = new File("/","autoexec.bat");
File f3 = new File(f1,"autoexec.bat");
NOTE  Java does the right thing with path separators between UNIX and Windows conventions.
OTE
If you use a forward slash (/) on a Windows version of Java, the path will still resolve correctly.
Remember, if you are using the Windows convention of a backslash character (\), you will need
to use its escape sequence (\\) within a string.
File defines many methods that obtain the standard properties of a File object. For example,
getName( ) returns the name of the file, getParent( ) returns the name of the parent directory,
and exists( ) returns true if the file exists, false if it does not. The File class, however, is not
symmetrical. By this, we mean that there are a few methods that allow you to examine the
properties of a simple file object, but no corresponding function exists to change those attributes.
The following example demonstrates several of the File methods:
// Demonstrate File.
import java.io.File;
class FileDemo {
static void p(String s) {
System.out.println(s);
}
public static void main(String args[]) {
File f1 = new File("/java/COPYRIGHT");
p("File Name: " + f1.getName());
p("Path: " + f1.getPath());
p("Abs Path: " + f1.getAbsolutePath());
p("Parent: " + f1.getParent());
p(f1.exists() ? "exists" : "does not exist");
p(f1.canWrite() ? "is writeable" : "is not writeable");
p(f1.canRead() ? "is readable" : "is not readable");
p("is " + (f1.isDirectory() ? "" : "not" + " a directory"));
p(f1.isFile() ? "is normal file" : "might be a named pipe");
p(f1.isAbsolute() ? "is absolute" : "is not absolute");
p("File last modified: " + f1.lastModified());
p("File size: " + f1.length() + " Bytes");
}
}
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home