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 along with satisfying the filter created with FilenameFilter interface. 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
* along with satisfying the filter.
* List2.java
* Author:-RoseIndia Team
* Date:-30-june-2008
*/
import java.io.*;
public class List2 {
public static void main(String args[]) {
File mind = new File("/home/mahendra/Desktop/move");
String[] move_list = mind.list();
int i = 0;
System.out.println("List of all files");
while (i < move_list.length) {
System.out.print(move_list[i++] + " ,");
}
System.out.println("");
System.out.println("");
System.out.println("Files excluding all 'docs (copy)' name files");
File over = new File("/home/baadshah/Desktop/move");
FilenameFilter hell_boy = new FilenameFilter() {
public boolean accept(File mind, String name) {
return !name.startsWith("docs (copy)");
}
};
String[] move_list1 = over.list(hell_boy);
int j = 0;
while (j < move_list1.length) {
System.out.print(move_list1[j++] + " ,");
}
System.out.println("");
System.out.println("");
System.out.println("Files including all 'rose' name files");
File matter = new File("/home/baadshah/Desktop/move");
FilenameFilter professor = new FilenameFilter() {
public boolean accept(File matter, String name) {
return name.startsWith("rose");
}
};
String[] move_list2 = matter.list(professor);
for (int k = 0; k < move_list2.length; k++) {
System.out.print(move_list2[k]);
}
}
} |
Output of the program:-
List of all files
main (copy) ,java2shtml (copy) ,docs (copy).wmv ,rose.wmv ,
Files excluding all 'docs (copy)' name files
main (copy) ,java2shtml (copy) ,rose.wmv ,
Files including all 'rose' name files
rose.wmv |
|