Skip to main content

Posts

Showing posts from May, 2015

Navigating folders and files in java

There are lot's of predefined libraries for File Management like Apache Commons. But it is better to know code that can be used without these libraries. Here is a small program to recursively search through a directory and print the name of all the files present in the directory.

import java.io.File;

/**
 *
 * @author Shankar
 *
 */

public class test {

  /**
   * @param args
   */
  public static void main(String[] args) {
    final File folder = new File("E:\\Openbravo\\ImportModule\\orderImport");
    listFiles(folder);
  }

  public static void listFiles(File folder) {
    for (final File currentFile : folder.listFiles()) {
      if (currentFile.isDirectory()) {
        listFiles(currentFile);
      } else {
        System.out.println(currentFile.getName());
      }
    }
  }
}

Notice that I have used double backward slash '\\' in file path as I am using windows (as strange as that might sound for an Ubuntu fanatic) ..:) . In linux single forward slash is enou…