Results 1 to 2 of 2
  1. #1
    Java Tip's Avatar
    Java Tip is offline Moderator
    Join Date
    Nov 2007
    Posts
    1,694
    Blog Entries
    430
    Rep Power
    9

    Default Listing file system from root directory

    The code below demonstrates how to list the file system root directories.

    Java Code:
    private static void doListRoots() {
    
            File[] roots = File.listRoots();
    
            for (int i=0; i<roots.length; i++) {
                System.out.println("Root[" + i + "] = " + roots[i]);
            }
        }

  2. #2
    smerchant is offline Member
    Join Date
    Apr 2009
    Posts
    1
    Rep Power
    0

    Default

    We can also use the following code to get all the subdirectories and files with any given folder.

    public class DirectoryReader {

    public static void populateArrayListsWithDirAndFilesInFolders(String currDirectory, ArrayList<String> directories, ArrayList<String> files )
    {
    File folder = new File(currDirectory);
    File[] listOfFiles = folder.listFiles();

    for (int i = 0; i < listOfFiles.length; i++)
    {
    if(listOfFiles[i].isFile())
    {
    files.add(listOfFiles[i].getPath());
    }
    if (listOfFiles[i].isDirectory())
    {
    directories.add(listOfFiles[i].getPath());
    //System.out.println("Directory name-" + listOfFiles[i].getName() + " path-" + listOfFiles[i].getPath());
    }
    }//End of for
    }//End of populateArrayListWithDirInFolders

    public static void getAllSubdirectoriesAndFiles(String basedir)
    {
    ArrayList<String> directories = new ArrayList<String>();
    ArrayList<String> files = new ArrayList<String>();
    directories.add(basedir);
    int currentLocationInArrayList = 1;
    while(currentLocationInArrayList<=directories.size ())
    {
    String currDirectory = directories.get(currentLocationInArrayList-1);
    currentLocationInArrayList++;
    populateArrayListsWithDirAndFilesInFolders(currDir ectory, directories, files );

    }//End of while

    System.out.println("*****\nFollowing are the files under the directory-" + basedir + "\n*****\n");
    for(String file : files)
    System.out.println(file);

    System.out.println("*****\nFollowing are the subdirectories under the directory-" + basedir + "\n*****\n");
    for(String directory : directories)
    System.out.println(directory);

    }//End of getAllSubdiectories

    public static void main(String args[])
    {
    getAllSubdirectoriesAndFiles("E://workspace//working//");

    }//End of main

    } //End of directoryReader

Similar Threads

  1. Is there a way to read a file directory
    By willemjav in forum Java Applets
    Replies: 3
    Last Post: 09-05-2011, 01:01 AM
  2. JSP/Servlet getting root directory
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 01-06-2009, 08:51 AM
  3. How to get Current Directory through File
    By Java Tip in forum java.io
    Replies: 0
    Last Post: 04-05-2008, 10:14 AM
  4. signaling file(s) have reached in directory
    By ashu261 in forum Advanced Java
    Replies: 0
    Last Post: 02-04-2008, 07:45 AM
  5. How to Move a File to Another Directory
    By Ada in forum New To Java
    Replies: 1
    Last Post: 05-26-2007, 01:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •