Source of ListFiles.java


  1: import java.util.Scanner;
  2: import java.io.File;

  4: public class ListFiles {

  6:     public static void main(String[] args) {
  7:         Scanner kbd = new Scanner(System.in);
  8:         String dirName;

 10:         System.out.print("Enter the name of a folder "
 11:                 + "and I'll show you the files in it.\n"
 12:                 + "Enter name here: ");
 13:         dirName = kbd.nextLine();
 14:         listFilesFrom(new File(dirName));
 15:     }

 17:     /**
 18:      * List all the files from the given directory file.
 19:      *
 20:      * @param dir the directory to list the files from.
 21:      */
 22:     private static void listFilesFrom(File dir) {
 23:         if (dir.isDirectory()) {
 24:             for (File file : dir.listFiles()) {
 25:                 System.out.println(file.getName());
 26:             }
 27:         } else {
 28:             System.out.println(dir.getName() + " is not a folder");
 29:         }
 30:     }

 32: }