Tuesday, January 1, 2013
Today I am going to post a program that will be able to display i.e list all the files and folders in your computer. Here we have used the java.io.File class for this purpose. At first we call the listRoots() function to list all the drives. Then for each drive we list all of their contents. A recursive function is defined that takes in a reference of File class instance as parameter. Now if it is a file then its absolute path is displayed. If it is a folder then all the contents of this directory is listed and kept in a File array. Now for each of the elements of the array the recursive function is called. The contents are listed following DFS algorithm.
--------------------------------------------------------------------------------------------------------------------------
Java Source Code
--------------------------------------------------------------------------------------------------------------------------


import java.io.File;
public class FileList {

    static void list(File f){
        if (f.isFile()){
            System.out.println(f.getAbsolutePath()); //displays path for a file
            return;
        }
        else{
            File dir_list[]=f.listFiles();  //lists all contents if a directory
            for (File t : dir_list)
                try{
                  list(t);  //calls list for each content of directory
                }catch(Exception e){
                  e.printStackTrace();
                }
        }
    }
   
    public static void main(String[] args){
        File drive_list[]=File.listRoots();  //lists all drives
           try{
               for(File drive : drive_list)
                  list(drive);  //calls recur for each drive
            }catch(Exception e){
              e.printStackTrace();
            }       
    }
}
--------------------------------------------------------------------------------------------------------------------------
Download Links
--------------------------------------------------------------------------------------------------------------------------
DOWNLOAD the source from Mediafire
DOWNLOAD the source from 4shared

0 comments:

Post a Comment

Total Pageviews

Subscribe via Email

Followers


Popular Posts

About Me

My photo

I am a student of BTech Computer Science Engineering from RCCIIT,Kolkata. I am a crazy lover of Java and wants to settle as a Java developer. I have a seven years Java experience with an application developer experience for 2 years. Recently from March 2012 I am a registered S40 app developer for Nokia and has corrected an app of them. I am currently writing blogs to encourage and grow interest in all those who don't know or learning Java.