Home Java Java-tips Io 10file Example - FileTest.java

Ask Questions?

View Latest Questions

Advertisement


 
 

Example - FileTest.java
Posted on: February 8, 2008 at 12:00 AM
This program prints many of the File charactistics.

Java Notes

Example - FileTest.java

 

This program prints many of the File charactistics.

This program takes the file name as argument and then prints the various characteristics of the file on console.

 

If file is present then if prints various characteristics of the file using following code:

 

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());
System.out.println("toURL()= " + f.toURL());
System.out.println("toURI()= " + f.toURI());
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())

 

Here is the complete code example:

 

// io/FileTest.java - Show values of some File methods.
// 2002-01-16 (Catania) -- Fred Swartz
import java.io.*;
import java.util.*;

class FileTest {
    public static void main(String[] args) {
        //-- Make sure there is one parameter
        if (args.length != 1) {
            System.err.println("ERROR: must have 1 parameter");
            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());
            System.out.println("toURL()            = " + f.toURL());
            System.out.println("toURI()            = " + f.toURI());
            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);
        }
    }//endmethod main
}//endclass FileTest
Copyleft 2003 Fred Swartz MIT License, Last update: 2003-11-01
Advertisement


DMCA.com