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

Followers


Labels

Popular Posts

free counters