Saturday, August 2, 2014

File List Util

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

public class FileList {

    public static File[] FileList(String directoryName) {
        File dir = new File(directoryName);
        // // Filter files, not to return any files that start with `.'.
        // FilenameFilter filter = new FilenameFilter() {
        // public boolean accept(File dir, String name) {
        // return !name.startsWith(".");
        // }
        // };
        // This filter only returns directories
        FileFilter fileFilter = new FileFilter() {
            public boolean accept(File file) {
                return file.isFile();
            }
        };
        File[] files = dir.listFiles(fileFilter);
        return files;
    } // end of method

    public static File[] DirectoryList(String directoryName) {
        File dir = new File(directoryName);
        // This filter only returns directories
        FileFilter fileFilter = new FileFilter() {
            public boolean accept(File file) {
                return file.isDirectory();
            }
        };
        File[] files = dir.listFiles(fileFilter);
        return files;
    } // end of method

    public static String getCurrentDirPath() {
        File dir1 = new File(".");
        String currentDir = null;
        try {
            currentDir = dir1.getCanonicalPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return currentDir;
    } // end of getCurrentDir()

    public static String getParent() {
        File dir1 = new File("..");
        String parrentDir = null;
        try {
            parrentDir = dir1.getCanonicalPath();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return parrentDir;
    } // end of getParrentDir()

    public static void createDirectoryIfNotExist(String directoryName) {
        File theDir = new File(directoryName);
          // if the directory does not exist, create it
          if (!theDir.exists())
          {
            boolean result = theDir.mkdir();
            if(result)            {  
               System.out.println("Directory created ... ");
             } // end of if
          }
        } // end of createDirectory()
   
    public static void main(String[] args) {
        String path = "C:\\";
        File[] fileList = FileList(path);
        System.out.println("File List .................... ");
        for (int i = 0; i < fileList.length; i++) {
            File file = fileList[i];
            System.out.println(file.toString());
        } // end of for

        System.out.println("Directory List ....................... ");
        fileList = DirectoryList(path);
        for (int i = 0; i < fileList.length; i++) {
            File file = fileList[i];
            System.out.println(file.toString());
        } // end of for

        System.out.println("Current Directory Path ....................... "
                + getCurrentDirPath());

        System.out
                .println("Parent Path ....................... " + getParent());
    } // end of main

} // end of class

No comments:

Post a Comment