Here is a Java Program to Demonstrate File.
Output of Above Java Program
File Name: COPYRIGHT
Path: \java\COPYRIGHT
Abs Path: C:\java\COPYRIGHT
Parent: \java
exists
is writeable
is readable
is
might be a named pipe
is not absolute
File last modified: 1329568044285
File size: 0 Bytes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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" ); } } |
Output of Above Java Program
File Name: COPYRIGHT
Path: \java\COPYRIGHT
Abs Path: C:\java\COPYRIGHT
Parent: \java
exists
is writeable
is readable
is
might be a named pipe
is not absolute
File last modified: 1329568044285
File size: 0 Bytes