Java File list directory contents

Introduction

The program shown here illustrates how to use list() to examine the folder content:


// Using directories. 
import java.io.File;

public class Main {
  public static void main(String args[]) {
    String dirname = "c:/";
    File f1 = new File(dirname);

    if (f1.isDirectory()) {
      System.out.println("Directory of " + dirname);
      String s[] = f1.list();//  ww w.j  a  v a  2 s.co  m

      for (int i = 0; i < s.length; i++) {
        File f = new File(dirname + "/" + s[i]);
        if (f.isDirectory()) {
          System.out.println(s[i] + " is a directory");
        } else {
          System.out.println(s[i] + " is a file");
        }
      }
    } else {
      System.out.println(dirname + " is not a directory");
    }
  }
}



PreviousNext

Related