#6: File objects represent files
The following program
returns various information about
a file that can't exist on any platform!
import java.io.*;
public class BadFile {
public static void main(String[] args) {
File f = new File("-What/a\\bad file name?!" + '\u0000');
System.out.println("getName: " + f.getName());
System.out.println("getPath: " + f.getPath());
System.out.println("getAbsolutePath: " + f.getAbsolutePath());
System.out.println("getParent: " + f.getParent());
if (f.isFile()) {
System.out.println(f.getName() + " is a file.");
}
else if (f.isDirectory()) {
System.out.println(f.getName() + " is a directory.");
}
else {
System.out.println("What is this?");
}
if (f.isAbsolute()) {
System.out.println(f.getName() + " is an absolute path.");
}
else {
System.out.println(f.getName() + " is not an absolute path.");
}
}
}
Output:
% java BadFile
getName: a\bad file name?!
getPath: -What/a\bad file name?!
getAbsolutePath: /export/sunsite/users/elharo/-What/a\bad file name?!
getParent: -What
What is this?
a\bad file name?! is not an absolute path.
Previous | Next | Top
Other Presentations
| Cafe au Lait Home
Last Modified May 16, 1999
Copyright 1999 Elliotte Rusty Harold
elharo@metalab.unc.edu