Getting directory contents from inside Java


I went searching for an example, found:
http://www.javafaq.nu/java-example-code-120.html
which shows several different ways to display content- everything, directories only, filter by string. I added a toy user interface so I can play with it interactively, resulting in a new main method:

public static void main(String[] args) {

String target = “//”;
String filter = “”;

if (args.length == 0) {
System.out.println(“java ListFilesSubs [first char(s) filter]”);
} else {
if (args.length > 0) {
// Call the simple file listing method
target = args[0];
doSimpleFileListing( target );
} // if args.lenl > 0

if (args.length > 1) {
// Now do the file listing but pass in a String to filter the file list
filter = args[1];
doFileFilterListing( target, filter );
} // if args.lenl > 1

// Now do another example that only prints out the directories
doFileFilterDirectoryListing( target );

} // if args.length == 0.. else

}
}

Much nicer, more fun, than a compiled-in directory string.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s