Home > Java > javaTutorial > body text

File filtering see java callback

巴扎黑
Release: 2016-12-20 16:55:08
Original
1355 people have browsed it

package file.callback;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
public class ListFile {
public static void main(String[] args) throws IOException {
filestrview();
}
public static void filestrview() {
try {
// 要过滤的文件所在位置
String FileURL = "D:/test";
File dir = new File(FileURL);
// 要过滤的文件类型,可以是任何类型文件的后缀名
String FileType = ".txt";
Filter filter = new Filter(FileType);
String filelist[] = dir.list(filter);
// 列出FileURL路径下的FileType类型的文件
for (int i = 0; i < filelist.length; i++) {
System.out.println("类型的文件: " + filelist[i]);
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
class Filter implements FilenameFilter {
String extension;
Filter(String extension) {
this.extension = extension;
}
// FilenameFilter接口的一个方法,必须实现它
public boolean accept(File directory, String filename)
{
return filename.endsWith(extension);
}
}
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!