file list Example
file class list example. This example shows you how to use list method.
File class list example. method returns string represented list of an array of files and directory files present inside abstract pathname directory.In case of file found instead of a directory this method returns 'null' & for an empty file it returns nothing.
Here is the code:-
/**
* @Program uses list method to list the directory contents.
* List1.java
* Author:-RoseIndia Team
* Date:-30-june-2008
*/
import java.io.*;
public class List1 {
public static void main(String args[]) {
File matrix = new File("/home/mahendra/Desktop/temp");
String[] array = matrix.list();
System.out.println("'temp' directory listing");
int i = 0;
if (matrix.isDirectory() == true) {
while (i < array.length) {
System.out.println(array[i]);
i++;
}
}
else if (matrix.isDirectory() == false) {
System.out.println("Abstract path name is not a directory: "
+ array);
}
if (array.length == 0) {
System.out.println("directory is empty");
}
System.out.println("");
System.out.println("'move' directory listing");
File matrix_reloaded = new File("/home/mahendra/Desktop/move");
String[] array_2D = matrix_reloaded.list();
if (matrix_reloaded.isDirectory() && array_2D.length != 0) {
for (int u = 0; u < array_2D.length; u++) {
System.out.println(array_2D[u]);
}
}
}
} |
Output of the program:-
'temp' directory listing
directory is empty
'move' directory listing
main (copy)
java2shtml (copy)
docs (copy).wmv
rose.wmv |
|