Find directories : Directory « File Input Output « Java






Find directories

Find directories
  
/*
   This program is a part of the companion code for Core Java 8th ed.
   (http://horstmann.com/corejava)

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;
import java.io.IOException;

/**
 * @version 1.00 05 Sep 1997
 * @author Gary Cornell
 */
public class FindDirectories
{
   public static void main(String[] args)
   {
      // if no arguments provided, start at the parent directory
      if (args.length == 0) args = new String[] { ".." };

      try
      {
         File pathName = new File(args[0]);
         String[] fileNames = pathName.list();

         // enumerate all files in the directory
         for (int i = 0; i < fileNames.length; i++)
         {
            File f = new File(pathName.getPath(), fileNames[i]);

            // if the file is again a directory, call the main method recursively
            if (f.isDirectory())
            {
               System.out.println(f.getCanonicalPath());
               main(new String[] { f.getPath() });
            }
         }
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
   }
}

   
    
  








Related examples in the same category

1.Create directory
2.Create directory along with required nonexistent parent directories
3.Create directory tree (nested/cascade folders)
4.Create a directories recursively
5.Copying a Directory: Copies files under srcDir to dstDir, if dstDir does not exist, it will be created.
6.Delete a non-empty directory: Deletes all files and subdirectories under dir.
7.Listing the Files or Subdirectories in a Directory
8.Listing the File System Roots
9.The Directory Listing ApplicationThe Directory Listing Application
10.use list( ) to examine the contents of a directory:
11.Reading and Printing a Directory HierarchyReading and Printing a Directory Hierarchy
12.Display a file system in a JTree viewDisplay a file system in a JTree view
13.File Tree DemoFile Tree Demo
14.File Table HTMLFile Table HTML
15.A standalone program that deletes a specified file or directory
16.Get Last modification time of a file or directory
17.Set last modified time of a file or directory
18.List contents of a directory
19.Determine if file or directory exists
20.Determine if File or Directory is hidden
21.Check if a directory is not empty
22.Get name of parent directory
23.Get name of specified file or directory
24.Get current directory
25.Mark file or directory Read Only
26.Rename file or directory
27.Traversing all files and directories under dir
28.Traversing only directories under dir
29.Traversing only files under dir
30.Creates and displays a window containing a list of files and sub-directories in a specified directory
31.Calculate directory size
32.Delete directory recursively
33.Determining If Two Filename Paths Refer to the Same File
34.Recursive directory deletion
35.Recursivly delete directory
36.Searches through the directory tree
37.Starts at the directory given and tests to see whether it is empty
38.Directory Walker
39.Utility methods for handling files and directories
40.Creates a new and empty directory in the default temp directory using the given prefix.
41.Count files in a directory (including files in all subdirectories)
42.Create a unique directory within a directory 'root'
43.Creates a new empty temporary directory.
44.Get Files Recurse
45.Recursively search a directory tree