FileGlobNio对查询指定的文件

nio中新增的性能

1.功能:对指定的路径下的指定的文件进行匹配查询

package com.lks.glob;

import java.io.IOException;
import java.nio.file.FileSystems;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * 功能:查询指定文件夹下的指定的格式的文件,匹配上的文件可以进行相应的业务处理
 * @author lks
 * @2017年2月8日
 * @上午11:37:15
 */
public class FileGlobNio
{
	public static void match(String glob, String location) throws IOException
	{

		final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob);

		Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>()
		{

			@Override
			public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException
			{
				//如果匹配上了指定的路径,处理查询的数据
				if (pathMatcher.matches(path))
				{
					System.out.println(path);
				}
				return FileVisitResult.CONTINUE;
			}

			@Override
			public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException
			{
				return FileVisitResult.CONTINUE;
			}
		});
	}

	public static void main(String args[]) throws IOException
	{
		String glob = "glob:**/*.txt";
		String path = "D:/";
		match(glob, path);
	}

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值