Monday, February 27, 2012

JAVA: Listing files


This article will teach you on how to get files on a specified directory and displaying its file name and size. Below are the following code:

import java.io.File;
public class TestFile {
    public static void main(String args[]) throws Exception {
          File folder = new File("D:/some folders");
          File[] listOfFiles = folder.listFiles();
          for (int i = 0; i < listOfFiles.length; i++) {
               if (listOfFiles[i].isFile()) {
                    System.out.println(listOfFiles[i].getName() 
                    + "," + listOfFiles[i].length() / 1024 + " KB");
               }
          }
     }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...

Blog List