Sunday, December 6, 2009

Java Examples

Computer directory listingImage via Wikipedia

Ever Google to figure out how to do something in java. Its always fun.
Try Googling:
file recursion java
you will get:
Results 1 - 10 of about 886,000 for file recursion java

A typical java subject search gives you everything you already know but not the one thing you need. You will get snippets of code. And often you will get Q/A threads asking how to do the same thing you are searching for with no reply. And you will see, copy after copy of it.

For those that found this page looking for java file recursion:

package blogsamples;

import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* File Recursioin example
*/
public class RecursiveFileListing {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
listAllFiles(new File(args[0]));
}
public static void listAllFiles(File dir) {
// if it is a directory get the contents.
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
// call itself
listAllFiles(new File(dir,children[i]));
}
} else {
// if it is a file print out the file.
try {
System.out.println(dir.getCanonicalPath());
} catch (IOException ex) {
Logger.getLogger(RecursiveFileListing.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}




Reblog this post [with Zemanta]

0 comments:

Post a Comment