We are moved to new domain
Click -> www.ehowtonow.com
Wednesday, 24 May 2017

How to traverse the directories using Java

This program explains how to traverse the directories and sub directories using Java
DirectoryTraversal.java
package com.javatutorialcorner.directory;

import java.io.File;

public class DirectoryTraversal {
  
    public static void traverseDirectory(File directory) {
       System.out.println(directory);
       if (directory.isDirectory()) {
          String[] subDirectory = directory.list();
          
          for (int i = 0; i < subDirectory.length; i++) {
             traverseDirectory(new File(directory, subDirectory[i])); 
          } 
       } 
    }
    
    public static void main(String[] argv) throws Exception { 
        System.out.println("Directory traversed : ");
        File directory = new File("C://JTC");
        traverseDirectory(directory);
     }

}
Output

Directory traversed :
C:\JTC
C:\JTC\Java
C:\JTC\Java\Examples
C:\JTC\Java\Examples\Directory
C:\JTC\Java\Examples\Directory\File

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to traverse the directories using Java Rating: 5 Reviewed By: eHowToNow