Last active
November 16, 2017 07:45
-
-
Save nbodev/6f654e35c423270cef42b9e41e2b0a0a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
public class TestGlob { | |
/* | |
* | |
say you have in the folder /home/me/Desktop, the following data folder | |
data | |
| | |
-test.csv | |
| | |
-newDirectory | |
| | |
--test2.csv | |
output of the program | |
====== glob1 glob:/home/me/Desktop/data/t*.csv | |
/home/me/Desktop/data/test.csv | |
====== glob2 glob:**t*.csv | |
/home/me/data/newDirectory/test2.csv | |
/home/me/data/test.csv | |
*/ | |
public static void main(final String args[]) throws IOException { | |
final String path = "/home/me/Desktop/data/"; | |
final String glob1 = "glob:" + path + "t*.csv";// first level only | |
final String glob2 = "glob:**t*.csv";// cross directory | |
System.out.println("====== glob1 " + glob1); | |
match(glob1, path); | |
System.out.println("====== glob2 " + glob2); | |
match(glob2, path); | |
} | |
public static void match(final String glob, final String location) throws IOException { | |
final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(glob); | |
Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() { | |
@Override | |
public FileVisitResult visitFile(final Path path, final BasicFileAttributes attrs) throws IOException { | |
if (pathMatcher.matches(path)) { | |
System.out.println(path); | |
} | |
return FileVisitResult.CONTINUE; | |
} | |
@Override | |
public FileVisitResult visitFileFailed(final Path file, final IOException exc) throws IOException { | |
return FileVisitResult.CONTINUE; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment