Thursday 24 May 2012

Load a file in Java

The following code shows how to load a file in Java using java.io.File class and retrieve the file attributes.




import java.io.File;

class LoadFile {

    public static void main(String args[]) {

        File file = new File("/home/radhakrishnan/t1.txt");

        System.out.println("Is File exists : " + file.exists());
        System.out.println("Is Directory : " + file.isDirectory());
        System.out.println("Is File : " + file.isFile());
        System.out.println("Is hidden file : " + file.isHidden());
        System.out.println("Absolute Path : " + file.getAbsolutePath());
        System.out.println("Parent : " + file.getParent());
        System.out.println("Path : " + file.getPath());
        System.out.println("FileName : " + file.getName());
        System.out.println("Last Modified : " + file.lastModified());
        System.out.println("File Length(in bytes) : " + file.length());
        System.out.println("Can Read : " + file.canRead());
        System.out.println("Can Write : " + file.canWrite());
        System.out.println("Can Execute : " + file.canExecute());

    }

}


The output is

Is File exists : true
Is Directory : false
Is File : true
Is hidden file : false
Absolute Path : /home/radhakrishnan/t1.txt
Parent : /home/radhakrishnan
Path : /home/radhakrishnan/t1.txt
FileName : t1.txt
Last Modified : 1337276056000
File Length(in bytes) : 6
Can Read : true
Can Write : true
Can Execute : false

No comments:

Post a Comment