This program explains how to traverse the directories and sub directories using Java
DirectoryTraversal.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
0 comments:
Post a Comment