1. Introduction
The list() method returns an array of pathnames strings that satisfy the specified filter. This method works simillar to list() method, except that returned array must satisfy the filter.
2. Method signature
public String[] list(FilenameFilter filter)
Parameters:
- FilenameFilter filter - a filename filter
Returns
- String
[]
- array of the pathnames
Throws
- SecurityException - when we do not have access to the file or directory
3. Examples
3.1. Code snippet that returns the pathnames of .txt
files from /tmp/ttt
directory
package com.frontbackend.java.io;
import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
public class FrontBackend {
public static void main(String args[]) {
try {
File tmp = new File("/tmp/ttt");
FilenameFilter filenameFilter = (file, s) -> s.endsWith(".txt");
String[] list = tmp.list(filenameFilter);
if (list != null) {
Arrays.stream(list)
.forEach(System.out::println);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
The output:
dest.txt
test.txt
4. Conclusion
In this article, we presented list(FilenameFilter filter) method that works similar to a simple list() but gives the ability to filter the resulted array.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}