FileGlobNio对查询指定的文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_15061629/article/details/61421390

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);
	}

}

猜你喜欢

转载自blog.csdn.net/qq_15061629/article/details/61421390