Pages

Friday, December 17, 2010

Java - FolderDir

/**
 *
 * This class is equivalen with DIR command in windows system.
 *
 * @author irfanudin ridho
 * @date december 19, 2010
 * @email irfan.ub@gmail.com
 * @version 1.0
 *
 */


import java.io.File;
import java.util.ArrayList;


class FolderDir{
   
    /*
     * Constructor of FolderDir
     * @param f String folder you search to know
     */
    public FolderDir(String f){
        file = new File(f);
        files = file.listFiles();

    }
   
    /**
     * method to get to know how many files in the directory
     *
     * @return int Count the files
     */
    public int getFilesCount(){
        int fileCount = 0;
        for(int i=0;i<files.length;i++){
            if(files[i].isFile()){
                fileCount = fileCount + 1;
            }
        }
        return fileCount;
    }
   
    /**
     * method to get to know how many folders in the directory
     *
     * @return int Count the folders
     */
    public int getFolderCount(){
        int folderCount = 0 ;
        for(int i=0;i<files.length;i++){
            if(files[i].isDirectory()){
                folderCount = folderCount + 1;
            }
        }
        return folderCount;
    }
   
    /**
     * method to list files as an array
     *
     * @return Array Array of Object for Files
     */
    public Object[] listFiles(){
        ArrayList<String> alFiles = new ArrayList<String>();
        for(int i=0;i<files.length;i++){
            if(files[i].isFile()){
                alFiles.add(files[i].toString());
            }
        }

        return alFiles.toArray();
    }
   
    /**
     * method to list folders as an array
     *
     * @return Array Array of Object for Folders
     */
    public Object[] listFolders(){
        ArrayList<String> alFolders = new ArrayList<String>();
        for(int i=0;i<files.length;i++){
            if(files[i].isDirectory()){
                alFolders.add(files[i].toString());
            }
        }

        return alFolders.toArray();
    }

    private File file;
    private File files[];
}

No comments:

Post a Comment