`

文件过滤 看 java 回调

    博客分类:
  • java
阅读更多
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);
	}
}

 

运行,输出结果为

类型的文件: a.txt
类型的文件: b.txt

 

查看源码:

 

public  interface FilenameFilter {
    /**
     * Tests if a specified file should be included in a file list.
     *
     * @param   dir    the directory in which the file was found.
     * @param   name   the name of the file.
     * @return  <code>true</code> if and only if the name should be
     * included in the file list; <code>false</code> otherwise.
     */
    boolean accept(File dir, String name);
}

 

File文件中的

/**
     * Returns an array of strings naming the files and directories in the
     * directory denoted by this abstract pathname that satisfy the specified
     * filter.  The behavior of this method is the same as that of the
     * <code>{@link #list()}</code> method, except that the strings in the
     * returned array must satisfy the filter.  If the given
     * <code>filter</code> is <code>null</code> then all names are accepted.
     * Otherwise, a name satisfies the filter if and only if the value
     * <code>true</code> results when the <code>{@link
     * FilenameFilter#accept}</code> method of the filter is invoked on this
     * abstract pathname and the name of a file or directory in the directory
     * that it denotes.
     *
     * @param  filter  A filename filter
     *
     * @return  An array of strings naming the files and directories in the
     *          directory denoted by this abstract pathname that were accepted
     *          by the given <code>filter</code>.  The array will be empty if
     *          the directory is empty or if no names were accepted by the
     *          filter.  Returns <code>null</code> if this abstract pathname
     *          does not denote a directory, or if an I/O error occurs.
     *
     * @throws  SecurityException
     *          If a security manager exists and its <code>{@link
     *          java.lang.SecurityManager#checkRead(java.lang.String)}</code>
     *          method denies read access to the directory
     */
    public String[] list(FilenameFilter filter) {
	String names[] = list();
	if ((names == null) || (filter == null)) {
	    return names;
	}
	ArrayList v = new ArrayList();
	for (int i = 0 ; i < names.length ; i++) {
	    if (filter.accept(this, names[i])) {
		v.add(names[i]);
	    }
	}
	return (String[])(v.toArray(new String[0]));
    }

 

很优雅.......

 

使用内部类 更优雅

 

http://www.blogjava.net/hwpok/archive/2008/04/01/190196.html

http://blog.sina.com.cn/s/blog_48cf38890100go6x.html

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics