See Java Notes: Java.io.File
The original program used a deprecated method.
The distinction between URI and URL can be confusing
Download revised program: FileTest.zip
FileTest.java
/** * io/FileTest.java - Show values of some File methods. * 2002-01-16 (Catania) -- Fred Swartz */ import java.io.*; import java.net.URI; import java.util.*; class FileTest { public static void main(String[] args) { //-- Make sure there is one parameter if (args.length != 1) { System.err.println("Usage: java FileTest filename"); System.exit(1); } try { File f = new File(args[0]); long d; System.out.println("getName() = " + f.getName()); System.out.println("getAbsoluteFile().getName() = " + f.getAbsoluteFile().getName()); boolean exists = f.exists(); System.out.println("exists() = " + exists); if (!exists) { System.exit(1); } System.out.println("canRead() = " + f.canRead()); System.out.println("canWrite() = " + f.canWrite()); System.out.println("getPath() = " + f.getPath()); System.out.println("getAbsolutePath() = " + f.getAbsolutePath()); System.out.println("getCanonicalPath() = " + f.getCanonicalPath()); System.out.println("getAbsoluteFile() = " + f.getAbsoluteFile()); // The following is deprecated. // System.out.println("toURL() = " + f.toURL()); URI uri = f.toURI(); System.out.println("toURI() = " + uri); System.out.println("convert URI to URL = " + uri.toURL()); System.out.println("getParent() = " + f.getParent()); System.out.println("isAbsolute() = " + f.isAbsolute()); boolean isDirectory = f.isDirectory(); System.out.println("isDirectory() = " + isDirectory); System.out.println("isFile() = " + f.isFile()); System.out.println("isHidden() = " + f.isHidden()); System.out.println("lastModified() = " + (d = f.lastModified()) + " = " + new Date(d)); System.out.println("length() = " + f.length()); if (isDirectory) { String[] subfiles = f.list(); for (int i=0; i<subfiles.length; i++) { System.out.println("file in this dir = " + subfiles[i]); } } } catch (IOException iox) { System.err.println(iox); } } }
C:\ece595_06\day06>javac -Xlint FileTest.java C:\ece595_06\day06>java FileTest FileTest.java getName() = FileTest.java getAbsoluteFile().getName() = FileTest.java exists() = true canRead() = true canWrite() = true getPath() = FileTest.java getAbsolutePath() = C:\ece595_06\day06\FileTest.java getCanonicalPath() = C:\ece595_06\day06\FileTest.java getAbsoluteFile() = C:\ece595_06\day06\FileTest.java toURI() = file:/C:/ece595_06/day06/FileTest.java convert URI to URL = file:/C:/ece595_06/day06/FileTest.java getParent() = null isAbsolute() = false isDirectory() = false isFile() = true isHidden() = false lastModified() = 1378576391779 = Sat Sep 07 13:53:11 EDT 2013 length() = 2592
Maintained by John Loomis, updated Sat Sep 07 13:54:23 2013