The following program reads filenames from the command line and
returns various information about those files using the methods of
the File
class.
import java.io.*;
public class FileInfo {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
File f = new File(args[i]);
if (f.exists()) {
System.out.println("getName: " + f.getName());
System.out.println("getPath: " + f.getPath());
System.out.println("getAbsolutePath: " + f.getAbsolutePath());
try {
System.out.println("getCanonicalPath: "
+ f.getCanonicalPath());
}
catch (IOException ex) {
}
System.out.println("getParent: " + f.getParent());
if (f.canWrite()) {
System.out.println(f.getName() + " is writable.");
}
if (f.canRead()) {
System.out.println(f.getName() + " is readable.");
}
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.");
}
try {
System.out.println("Last Modified" + f.lastModified());
System.out.println(f.getName() + " is "
+ f.length() + " bytes.");
System.out.println(f.getName() + " is "
+ f.length() + " bytes.");
}
catch (IOException ex) {
}
}
else {
System.out.println("Can't find the file " + args[i]);
}
}
}
}