File Map
This example shows how to list the files of user given folder as a
text map having tree hierarchy.
package com.bethecoder.tutorials.core.gen;
import java.io.*;
import java.util.*;
/**
* Shows file system as map.
*
* @author sudhakar
* @version 1.0
*/
public class FileMap {
public static void main ( String args [] ) throws Exception {
displayFiles ( "C:\\JackRabbitTests" ) ;
} /* end main */
private static final int INITIAL_DEPTH = 1 ;
private static final String DEFAULT_SEP_WIDTH = " " ;
private static final String FILE_PREFIX = "---" ;
public static void displayFiles ( String filePath ) {
displayFiles ( new File ( filePath )) ;
}
public static void displayFiles ( File file ) {
System.out.print ( file ) ;
List<Integer> depthList = new Vector<Integer> () ;
depthList.add ( new Integer ( INITIAL_DEPTH )) ;
displayFiles ( file.listFiles () , INITIAL_DEPTH, depthList ) ;
}
public static void displayFiles ( File [] files, int depth, List<Integer> parentDepths ) {
Integer maxDepth = ( Integer ) parentDepths.get ( parentDepths.size () - 1 ) ;
String hline = "" ;
for ( int i = INITIAL_DEPTH ; i <= maxDepth ; i ++ ) {
boolean found = false ;
for ( int j = 0 ; j < parentDepths.size () ; j ++ ) {
Integer curDepth = ( Integer ) parentDepths.get ( j ) ;
if ( i == curDepth.intValue ()) {
found = true ;
}
}
if ( found == true ) {
hline += DEFAULT_SEP_WIDTH + "|" ;
} else {
hline += DEFAULT_SEP_WIDTH;
}
}
/**
* Required format
*
*
* |
* |-- File name
*/
hline = "\n" + hline + "\n" + hline ;
hline += FILE_PREFIX;
for ( int i = 0 ; i < files.length ; i ++ ) {
if ( files [ i ] .isFile ()) {
//print file
System.out.print ( hline + files [ i ] .getName ()) ;
} else {
//print directory
System.out.print ( hline + File.separator + files [ i ] .getName ()) ;
/**
* If this is last file in this directory then no need
* of showing parent chain further.
*
* Remove parent depth from depth list
*/
List<Integer> depthList = new Vector<Integer> ( parentDepths ) ;
Integer curDepth = new Integer ( depth ) ;
Integer nxtLevelDepth = new Integer ( depth+ 1 ) ;
//last file in this directory
if ( i == files.length- 1 ) {
if ( depthList.contains ( curDepth )) {
depthList.remove ( curDepth ) ;
}
}
//add next level depth
depthList.add ( nxtLevelDepth ) ;
if ( files [ i ] .listFiles () != null ) {
displayFiles ( files [ i ] .listFiles () , nxtLevelDepth, depthList ) ;
}
}
}
} /* end of displayFiles method */
} /* end of FileMap class */
It gives the following output,
C:\JackRabbitTests
|
|---.classpath
|
|---.project
|
|---\.settings
| |
| |---org.eclipse.jdt.core.prefs
| |
| |---org.eclipse.jdt.ui.prefs
|
|---\bin
| |
| |---.classpath
| |
| |---.project
| |
| |---\.settings
| | |
| | |---org.eclipse.jdt.core.prefs
| | |
| | |---org.eclipse.jdt.ui.prefs
| |
| |---jackrabbit-standalone-1.6.0.jar
| |
| |---\org
| | |
| | |---\library
| | | |
| | | |---\test
| | | |
| | | |---Book.class
| | | |
| | | |---Library.class
| | | |
| | | |---LibUtil.class
| | |
| | |---\test
| | |
| | |---RepoTest.class
| |
| |---repository.xml
| |
| |---Test.class
|
|---derby.log
|
|---jackrabbit-standalone-1.6.0.jar
|
|---\org
| |
| |---\library
| | |
| | |---\test
| | |
| | |---Book.java
| | |
| | |---Library.java
| | |
| | |---LibUtil.java
| |
| |---\test
| |
| |---RepoTest.java
|
|---\repository
| |
| |---\repository
| | |
| | |---\index
| | | |
| | | |---indexes
| | | |
| | | |---\_0
| | | |
| | | |---segments.gen
| | | |
| | | |---segments_1
| | | |
| | | |---segments_2
| | | |
| | | |---_0.cfs
| | |
| | |---\meta
| | | |
| | | |---rep.properties
| | | |
| | | |---rootUUID
| | |
| | |---\namespaces
| | | |
| | | |---ns_idx.properties
| | | |
| | | |---ns_reg.properties
| | |
| | |---\nodetypes
| |
| |---\version
| | |
| | |---\db
| | |
| | |---\log
| | | |
| | | |---log.ctrl
| | | |
| | | |---log1.dat
| | | |
| | | |---logmirror.ctrl
| | |
| | |---\seg0
| | | |
| | | |---c150.dat
| | | |
| | | |---c161.dat
| | | |
| | | |---c171.dat
| | | |
| | | |---c1a1.dat
| | |
| | |---service.properties
| |
| |---\versions
| | |
| | |---\blobs
| | |
| | |---\data
| | |
| | |---\dead
| | |
| | |---\beef
| | |
| | |---\facebabecafebabecafebabe
| | |
| | |---.node.xml
| | |
| | |---672388333465d36a71297afb5d46001f.xml
| |
| |---\workspaces
| |
| |---\default
| |
| |---\blobs
| |
| |---\data
| | |
| | |---\cafe
| | | |
| | | |---\babe
| | | |
| | | |---\cafebabecafebabecafebabe
| | | |
| | | |---.node.xml
| | | |
| | | |---672388333465d36a71297afb5d46001f.xml
| | |
| | |---\dead
| | |
| | |---\beef
| | |
| | |---\cafebabecafebabecafebabe
| | |
| | |---.node.xml
| | |
| | |---672388333465d36a71297afb5d46001f.xml
| |
| |---\index
| |
| |---workspace.xml
|
|---repository.xml
|
|---Test.java