本文整理汇总了Java中java.nio.file.PathMatcher.matches方法的典型用法代码示例。如果您正苦于以下问题:Java PathMatcher.matches方法的具体用法?Java PathMatcher.matches怎么用?Java PathMatcher.matches使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.PathMatcher
的用法示例。
在下文中一共展示了PathMatcher.matches方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: loadDatabaseReaders
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
static Map<String, DatabaseReaderLazyLoader> loadDatabaseReaders(Path geoIpConfigDirectory, NodeCache cache) throws IOException {
if (Files.exists(geoIpConfigDirectory) == false && Files.isDirectory(geoIpConfigDirectory)) {
throw new IllegalStateException("the geoip directory [" + geoIpConfigDirectory + "] containing databases doesn't exist");
}
Map<String, DatabaseReaderLazyLoader> databaseReaders = new HashMap<>();
try (Stream<Path> databaseFiles = Files.list(geoIpConfigDirectory)) {
PathMatcher pathMatcher = geoIpConfigDirectory.getFileSystem().getPathMatcher("glob:**.mmdb.gz");
// Use iterator instead of forEach otherwise IOException needs to be caught twice...
Iterator<Path> iterator = databaseFiles.iterator();
while (iterator.hasNext()) {
Path databasePath = iterator.next();
if (Files.isRegularFile(databasePath) && pathMatcher.matches(databasePath)) {
String databaseFileName = databasePath.getFileName().toString();
DatabaseReaderLazyLoader holder = new DatabaseReaderLazyLoader(databaseFileName, () -> {
try (InputStream inputStream = new GZIPInputStream(Files.newInputStream(databasePath, StandardOpenOption.READ))) {
return new DatabaseReader.Builder(inputStream).withCache(cache).build();
}
});
databaseReaders.put(databaseFileName, holder);
}
}
}
return Collections.unmodifiableMap(databaseReaders);
}
示例2: read
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
/**
* Reads in the given JSON file and parses the content.
* Creates and returns a new settings object with it.
*
* @param path path to JSON file
* @return settings object or null if impossible to read
* @throws IllegalArgumentException if the given path is null
* @throws FileNotFoundException if the given path can not be found
*/
public static Settings read(Path path) throws IllegalArgumentException, FileNotFoundException {
if (path == null) {
throw new IllegalArgumentException("The given file path is not initialized.");
}
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.json");
if (!matcher.matches(path)) {
throw new IllegalArgumentException("The file ending does not match .json.");
}
Settings settings = null;
// parse JSON document to a java object
JSONSettings json = new Gson().fromJson(new FileReader(path.toFile()), JSONSettings.class);
if (json != null) {
// create the actual settings object with the information of the read in objects
settings = new Settings(json);
}
return settings;
}
示例3: TaildirMatcher
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
/**
* Package accessible constructor. From configuration context it represents a single
* <code>filegroup</code> and encapsulates the corresponding <code>filePattern</code>.
* <code>filePattern</code> consists of two parts: first part has to be a valid path to an
* existing parent directory, second part has to be a valid regex
* {@link java.util.regex.Pattern} that match any non-hidden file names within parent directory
* . A valid example for filePattern is <code>/dir0/dir1/.*</code> given
* <code>/dir0/dir1</code> is an existing directory structure readable by the running user.
* <p></p>
* An instance of this class is created for each fileGroup
*
* @param fileGroup arbitrary name of the group given by the config
* @param filePattern parent directory plus regex pattern. No wildcards are allowed in directory
* name
* @param cachePatternMatching default true, recommended in every setup especially with huge
* parent directories. Don't set when local system clock is not used
* for stamping mtime (eg: remote filesystems)
* @see TaildirSourceConfigurationConstants
*/
TaildirMatcher(String fileGroup, String filePattern, boolean cachePatternMatching) {
// store whatever came from configuration
this.fileGroup = fileGroup;
this.filePattern = filePattern;
this.cachePatternMatching = cachePatternMatching;
// calculate final members
File f = new File(filePattern);
this.parentDir = f.getParentFile();
String regex = f.getName();
final PathMatcher matcher = FS.getPathMatcher("regex:" + regex);
this.fileFilter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return matcher.matches(entry.getFileName()) && !Files.isDirectory(entry);
}
};
// sanity check
Preconditions.checkState(parentDir.exists(),
"Directory does not exist: " + parentDir.getAbsolutePath());
}
示例4: readFileWithPattern
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
public static List<String> readFileWithPattern(File path) {
PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:" + path.getAbsolutePath());
List<String> result = new ArrayList<String>();
String p = path.getAbsolutePath();
int index = p.lastIndexOf(File.separator);
if(index == -1) {
throw new IllegalArgumentException("Cannot parse path:" + p + ", please use like this : /f/example/*.bgz ");
}
File dir = new File(p.substring(0, index));
File[] files = dir.listFiles();
for (File file : files) {
if(pathMatcher.matches(file.toPath())) {
result.add(file.getAbsolutePath());
}
}
// if(result.size() > MAX_FILE) {
// throw new IllegalArgumentException("Cannot handle files more than " + MAX_FILE + " at one time, please merge file first." );
// }
return result;
}
示例5: newDirectoryStream
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
/**
* Opens a directory stream on provided 'dir' folder, filtering file names according
* to the provided glob syntax.
* <p>
* See http://blog.eyallupu.com/2011/11/java-7-working-with-directories.html
*
* @param dir the directory to read from
* @param glob the glob matching
* @return the opened DirectoryStream (remaining to be closed)
* @throws IOException
*/
public static DirectoryStream newDirectoryStream (Path dir,
String glob)
throws IOException
{
// create a matcher and return a filter that uses it.
final FileSystem fs = dir.getFileSystem();
final PathMatcher matcher = fs.getPathMatcher("glob:" + glob);
final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>()
{
@Override
public boolean accept (Path entry)
{
return matcher.matches(entry.getFileName());
}
};
return fs.provider().newDirectoryStream(dir, filter);
}
示例6: filterPath
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
@Override
public boolean filterPath(Path filePath) {
if (getIncludeMatchers().isEmpty() && getExcludeMatchers().isEmpty()) {
return false;
}
// compensate for the fact that Flink paths are slashed
final String path = filePath.hasWindowsDrive() ?
filePath.getPath().substring(1) :
filePath.getPath();
final java.nio.file.Path nioPath = Paths.get(path);
for (PathMatcher matcher : getIncludeMatchers()) {
if (matcher.matches(nioPath)) {
return shouldExclude(nioPath);
}
}
return true;
}
示例7: visitFile
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
@Override
public FileVisitResult visitFile(Path path, BasicFileAttributes arg1) throws IOException {
FileSystem fileSystem = FileSystems.getDefault();
PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:" + filePattern);
if (pathMatcher.matches(path.getFileName())) {
searchList.add(path);
String charset = Charset.defaultCharset().name();
//System.out.println(charset);
String content = new String(Files.readAllBytes(path), charset);
content = content.replaceAll(this.searchPattern, this.replacePattern);
Files.write(path, content.getBytes(charset));
fileCount++;
}
return FileVisitResult.CONTINUE;
}
示例8: parse
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
/**
* Parse all IGFS log files in specified log directory.
*
* @param logDir Folder were log files located.
* @return List of line with aggregated information by files.
*/
private List<VisorIgfsProfilerEntry> parse(Path logDir, String igfsName) throws IOException {
List<VisorIgfsProfilerEntry> parsedFiles = new ArrayList<>(512);
try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(logDir)) {
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:igfs-log-" + igfsName + "-*.csv");
for (Path p : dirStream) {
if (matcher.matches(p.getFileName())) {
try {
parsedFiles.addAll(parseFile(p));
}
catch (NoSuchFileException ignored) {
// Files was deleted, skip it.
}
catch (Exception e) {
ignite.log().warning("Failed to parse IGFS profiler log file: " + p, e);
}
}
}
}
return parsedFiles;
}
示例9: getMatchingFiles
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
/**
* Returns a list of all files matching the given pattern. If {@code recursive} is true
* will recurse into all directories below {@code rootPath}.
*/
public static List<Path> getMatchingFiles(final Path rootPath, String globPattern, boolean recursive)
throws IOException {
final List<Path> result = new ArrayList<>();
if (!recursive) {
DirectoryStream<Path> dirStream = getMatchingFiles(rootPath, globPattern);
for (Path path : dirStream) {
result.add(path);
}
} else {
final PathMatcher matcher = FileSystems.getDefault().getPathMatcher(globPattern);
FileVisitor<Path> visitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attribs) {
if (matcher.matches(file.getFileName())) {
result.add(rootPath.relativize(file));
}
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(rootPath, visitor);
}
return result;
}
示例10: validateInitialFileToProcess
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
private void validateInitialFileToProcess(List<ConfigIssue> issues) {
if (conf.initialFileToProcess != null && !conf.initialFileToProcess.isEmpty()) {
try {
PathMatcher pathMatcher = DirectorySpooler.createPathMatcher(conf.filePattern, conf.pathMatcherMode);
if (!pathMatcher.matches(new File(conf.initialFileToProcess).toPath().getFileName())) {
issues.add(
getContext().createConfigIssue(
Groups.FILES.name(),
SPOOLDIR_CONFIG_BEAN_PREFIX + "initialFileToProcess",
Errors.SPOOLDIR_18,
conf.initialFileToProcess,
conf.filePattern
)
);
}
} catch (Exception ex) {
}
}
}
示例11: visitFile
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
Objects.nonNull(file);
Objects.nonNull(attrs);
Path found = file.getFileName();
for (String name : matchers.keySet())
if (!results.containsKey(name)) {
PathMatcher matcher = matchers.get(name);
if (null != found && matcher.matches(found) && Files.isExecutable(file) && check(file, name, version)) {
results.put(name, file);
if (results.size() == matchers.size())
return FileVisitResult.TERMINATE;
}
}
return FileVisitResult.CONTINUE;
}
示例12: searchFiles
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
public static List<Path> searchFiles(Path path, String globPattern) throws IOException {
if (Files.isDirectory(path)) {
Finder finder = new Finder(globPattern);
Files.walkFileTree(path, finder);
return finder.getPaths();
} else {
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + globPattern);
if (matcher.matches(path.getFileName())) {
return Arrays.asList(path);
} else {
return Collections.emptyList();
}
}
}
示例13: FindK
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
public static int FindK(int numFiles, Integer pic, String[] str) throws NotFound {
String pattern = pic.toString() + "_";
int k;
PathMatcher matcher =
FileSystems.getDefault().getPathMatcher("glob:" + pattern + "*");
//System.out.println("pattern ="+pattern);
for (k = 0; k < numFiles; k++) {
//System.out.println("pattern ="+pattern);
Path filename = new File(str[k]).toPath();
//System.out.println(filename);
if (matcher.matches(filename)) {
//System.out.println("yes pattern " + filename);
break;
}
}
if (k == numFiles || numFiles == 0) {
throw new NotFound("k non trovato");
}
return k;
}
示例14: listJavaScript
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
private List<String> listJavaScript(String base, String ext)
throws IOException {
final List<String> list = new ArrayList<String>();
FileSystem fs = FileSystems.getDefault();
PathMatcher matcher = fs.getPathMatcher("glob:" + ext);
FileVisitor<Path> matcherVisitor = new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attribs) {
Path name = file.getFileName();
if (matcher.matches(name)) {
list.add(file.toString());
}
return FileVisitResult.CONTINUE;
}
};
Files.walkFileTree(Paths.get(base), matcherVisitor);
return list;
}
示例15: pathMatcher
import java.nio.file.PathMatcher; //导入方法依赖的package包/类
private static PathMatcher pathMatcher(final String expressions) {
List<PathMatcher> matchers = new ArrayList<>();
for (String expression : expressions.split(File.pathSeparator)) {
matchers.add(FileSystems.getDefault().getPathMatcher("glob:" + expression.trim()));
}
return new PathMatcher() {
@Override
public boolean matches(final Path path) {
for (PathMatcher matcher : matchers) {
if (matcher.matches(path)) {
return true;
}
}
return false;
}
@Override
public String toString() {
return "[" + expressions + "]";
}
};
}