`

文件过滤 看 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

 

 

分享到:
评论

相关推荐

    ajax实现java文件下载

    1. **前端**:在JSP文件中,使用JavaScript创建一个Ajax请求,设置请求方法(GET或POST)、URL(指向FileAction)、数据(如文件扩展名)以及成功和失败回调函数。 2. **后端**:FileAction接收到请求后,解析请求...

    FileFitle(文件过滤)

    Java的`java.io.File`类有`listFiles()`方法,配合文件过滤器(如`FilenameFilter`)可实现过滤。在JavaScript中,Node.js的`fs.readdir()`配合回调函数或Promise可以实现类似功能。 4. **正则表达式过滤**: 对于...

    简单的回调demo

    在JavaScript、Java、C#等编程语言中,回调都是一个非常基础的概念。让我们深入探讨一下回调的原理和应用。 1. 回调的基本概念: - 回调函数:一个可以在其他函数执行完毕后被调用的函数,通常作为参数传递给另一...

    IO 目录列表器 回调函数 Thinking in java 4

    "IO 目录列表器 回调函数 Thinking in java 4"这个标题涉及到一个关于使用Java进行目录遍历并处理文件的场景。在这个场景中,我们可能会用到`java.io.File`类以及回调函数的概念。回调函数是一种编程设计模式,它...

    java带进度条大文件上传

    `FileUploadListener.java`可能就是用来实现这一功能的类,它可能会包含文件上传事件监听和进度回调的方法。 4. **后台服务**:`BackGroundService.java`可能是一个后台服务类,负责在后台执行文件上传任务,这样...

    Java 文件监控,实时监控文件加载

    Apache Commons IO 提供的`FileAlterationObserver`是一个强大的文件监控工具,它不仅能够监听文件系统的变动,还能设定监控间隔、回调函数等。下面是一个使用`FileAlterationObserver`的例子: ```java import org...

    xhEditor文件上传的Java实现.pdf

    《xhEditor文件上传的...总之,xhEditor的Java文件上传实现涉及前端的编辑器配置、回调函数处理,以及后端的Servlet开发和JSON数据交互。正确配置和实现这些环节,才能确保用户能够顺利地在xhEditor中上传和使用图片。

    MacOSX-FileSystem-Filter:Mac OS X的文件系统过滤器

    它不允许过滤读写操作,并且由于在kauth回调调用时已经创建了vnode,因此对文件系统操作提供了有限的控制。 在Windows中,kauth回调是用于创建/打开请求(对于Windows为IRP_MJ_CREATE)的后操作回调,对于Mac OS X...

    java swfupload,上传,图片上传,文件上传,批量上传

    6. **进度显示**:SWFUpload支持文件上传进度的显示,需要在前端JavaScript和后端Java之间建立通信机制,比如使用Ajax回调或者WebSocket,实时更新上传进度。 7. **用户体验**:为了提供良好的用户体验,可以使用...

    Java开发技术大全(500个源代码).

    hasRecall.java 可以完成回调功能的类 HasStatic.java 一个简单的拥有静态成员的类 hideMember_1.java 成员隐藏示例1 hideMember_2.java 成员隐藏示例2 hideMember_3.java 成员隐藏示例3 hideMember_4.java ...

    java开源项目(2)Java File Copy Library

    3. **进度监控**:该库提供了复制进度回调机制,开发者可以通过注册监听器来获取复制过程中的实时进度信息,这对于用户界面的更新或者日志记录非常有用。 4. **错误处理**:在文件复制过程中,可能会遇到各种异常,...

    SwfUpload结合Java

    6. **回调函数**:SwfUpload支持自定义JavaScript回调函数,这些函数会在上传过程的各个阶段被调用,比如文件选择、上传开始、上传进度和上传完成等。你可以利用这些回调来更新界面状态,提供动态反馈。 7. **...

    uploadify java完整项目工程

    "uploadify java完整项目工程"是一个基于JSP、Servlet和Java技术实现的文件上传解决方案,主要特点是使用了...在实际应用中,你可能还需要根据具体需求调整Uploadify的配置,例如设置上传大小限制、回调函数等。

    网上购物系统Java带数据库文件

    这涉及到API调用、支付状态回调处理等技术。 9. **用户认证与授权**:系统应包含用户注册、登录功能,并实现权限管理,如基于角色的访问控制(RBAC),确保不同用户只能访问和操作他们被允许的数据。 10. **测试与...

    CoreJava高级部分

    本篇文档主要围绕Java IO包中的`java.io.File`类进行深入探讨,涉及到了文件和目录的操作、属性查询以及如何使用接口回调模式处理特定问题等方面的内容。 #### 1. `java.io.File`类简介 `java.io.File`类主要用于...

    官网下载java 64位1.8版本JDK

    3. **Lambda表达式**:Lambda表达式是Java 8中的一个核心特性,它提供了一种简洁的方式来表示匿名函数,简化了处理函数接口和回调函数的代码。 4. **方法引用**:方法引用允许直接引用已有方法,而不是通过lambda...

    java-jdk1.8-jdk-8u171-windows-x64.zip

    通过使用lambda,开发者可以更简洁地定义匿名函数,这在处理高阶函数和回调时特别有用。 其次,Java 8引入了方法引用来代替传统的匿名内部类,这使得代码更加简洁。此外,还新增了接口的默认方法和静态方法,使得...

    JAVA IO流小结

    JAVA IO流小结 JAVA IO流是指Java语言中用来实现输入/输出操作的机制。IO流是指任何有能力...回调是一种设计模式,允许一个类提供一些方法给其他类用,但同时其他类在调用它时,它又调用其他类给它的条件(重写)。

    jdk1.8,亲测可用 jdk sdk java

    1. **lambda表达式**:这是Java 8最重要的特性之一,它引入了函数式编程的概念,允许开发者以更简洁的方式处理集合和回调函数。Lambda表达式可以被用作方法参数,简化了匿名内部类的使用。 2. **方法引用和构造器...

Global site tag (gtag.js) - Google Analytics