本文整理匯總了Java中java.nio.file.Files.walk方法的典型用法代碼示例。如果您正苦於以下問題:Java Files.walk方法的具體用法?Java Files.walk怎麽用?Java Files.walk使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.nio.file.Files
的用法示例。
在下文中一共展示了Files.walk方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getConfig
import java.nio.file.Files; //導入方法依賴的package包/類
@Override
public Config getConfig() throws IOException {
PathMatcher pathMatcher;
try {
pathMatcher = FileSystems.getDefault().getPathMatcher(inputFilePattern);
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
"Invalid input file pattern: " + inputFilePattern);
}
try (Stream<Path> pathStream = Files.walk(baseDirectory)) {
return pathStream
.filter(p -> Files.isRegularFile(p) && pathMatcher.matches(p))
.map(p -> ConfigFactory.parseFile(p.toFile()))
.reduce(ConfigFactory.empty(), Config::withFallback)
.resolve(
ConfigResolveOptions.defaults()
.setAllowUnresolved(true)
.setUseSystemEnvironment(false)
);
}
}
示例2: processSelf
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Processes the class files from the currently running JDK,
* using the jrt: filesystem.
*
* @return true for success, false for failure
* @throws IOException if an I/O error occurs
*/
boolean processSelf(Collection<String> classes) throws IOException {
options.add("--add-modules");
options.add("java.se.ee,jdk.xml.bind"); // TODO why jdk.xml.bind?
if (classes.isEmpty()) {
Path modules = FileSystems.getFileSystem(URI.create("jrt:/"))
.getPath("/modules");
// names are /modules/<modulename>/pkg/.../Classname.class
try (Stream<Path> paths = Files.walk(modules)) {
Stream<String> files =
paths.filter(p -> p.getNameCount() > 2)
.map(p -> p.subpath(1, p.getNameCount()))
.map(Path::toString);
return doModularFileNames(files);
}
} else {
return doClassNames(classes);
}
}
示例3: copyClasses
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Copy classes except the module-info.class to the destination directory
*/
public static void copyClasses(Path from, Path dest) throws IOException {
try (Stream<Path> stream = Files.walk(from, Integer.MAX_VALUE)) {
stream.filter(path -> !path.getFileName().toString().equals(MODULE_INFO) &&
path.getFileName().toString().endsWith(".class"))
.map(path -> from.relativize(path))
.forEach(path -> {
try {
Path newFile = dest.resolve(path);
Files.createDirectories(newFile.getParent());
Files.copy(from.resolve(path), newFile);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}
示例4: compileAll
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* Compiles all modules used by the test
*/
@BeforeTest
public void compileAll() throws Exception {
CompilerUtils.cleanDir(MODS_DIR);
CompilerUtils.cleanDir(LIBS_DIR);
for (String mn : modules) {
// compile a module
assertTrue(CompilerUtils.compileModule(SRC_DIR, MODS_DIR, mn));
// create JAR files with no module-info.class
Path root = MODS_DIR.resolve(mn);
try (Stream<Path> stream = Files.walk(root, Integer.MAX_VALUE)) {
Stream<Path> entries = stream.filter(f -> {
String fn = f.getFileName().toString();
return fn.endsWith(".class") && !fn.equals("module-info.class");
});
JdepsUtil.createJar(LIBS_DIR.resolve(mn + ".jar"), root, entries);
}
}
}
示例5: testWalkOneLevel
import java.nio.file.Files; //導入方法依賴的package包/類
public void testWalkOneLevel() {
try (Stream<Path> s = Files.walk(testFolder, 1)) {
Object[] actual = s.filter(path -> ! path.equals(testFolder))
.sorted()
.toArray();
assertEquals(actual, level1);
} catch (IOException ioe) {
fail("Unexpected IOException");
}
}
示例6: DirectoryIterator
import java.nio.file.Files; //導入方法依賴的package包/類
DirectoryIterator() throws IOException {
List<Path> paths = null;
try (Stream<Path> stream = Files.walk(path, Integer.MAX_VALUE)) {
paths = stream.filter(ClassFileReader::isClass)
.collect(Collectors.toList());
}
this.entries = paths;
this.index = 0;
}
示例7: collectClassFile
import java.nio.file.Files; //導入方法依賴的package包/類
List<String> collectClassFile(Path root) throws IOException {
try (Stream<Path> files = Files.walk(root)) {
return files.filter(p -> Files.isRegularFile(p))
.filter(p -> p.getFileName().toString().endsWith(".class"))
.map(p -> root.relativize(p).toString())
.collect(Collectors.toList());
}
}
示例8: gatherClasses
import java.nio.file.Files; //導入方法依賴的package包/類
private ResourcePool gatherClasses(Path module) throws Exception {
ResourcePoolManager poolMgr = new ResourcePoolManager(ByteOrder.nativeOrder(), new StringTable() {
@Override
public int addString(String str) {
return -1;
}
@Override
public String getString(int id) {
return null;
}
});
ResourcePoolBuilder poolBuilder = poolMgr.resourcePoolBuilder();
try (Stream<Path> stream = Files.walk(module)) {
for (Iterator<Path> iterator = stream.iterator(); iterator.hasNext();) {
Path p = iterator.next();
if (Files.isRegularFile(p) && p.toString().endsWith(".class")) {
byte[] content = Files.readAllBytes(p);
poolBuilder.add(ResourcePoolEntry.create(p.toString(), content));
}
}
}
return poolBuilder.build();
}
示例9: start
import java.nio.file.Files; //導入方法依賴的package包/類
/**
* This method launches the CommunicationManager
*
* @throws IllegalArgumentException if communication.path is not set
*/
public void start() {
String stringPath = getPathCommunication();
if (stringPath == null) return;
try (Stream<Path> paths = Files.walk(Paths.get(stringPath))) {
paths.filter(Files::isRegularFile)
.filter(file -> file.toString().endsWith(".jar"))
.forEach((Path filePath) -> {
JarLoader jarLoader = JarLoader.createJarLoader(filePath.toString());
launchModule(jarLoader);
});
} catch (IOException e) {
LOGGER.error(e.getMessage());
}
LOGGER.info("All ICommunication has been launched");
}
示例10: main
import java.nio.file.Files; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
String javaHome = args[0];
FileSystem fs = null;
boolean isInstalled = false;
if (args.length == 2) {
fs = createFsByInstalledProvider();
isInstalled = true;
} else {
fs = createFsWithURLClassloader(javaHome);
}
Path mods = fs.getPath("/modules");
try (Stream<Path> stream = Files.walk(mods)) {
stream.forEach(path -> {
path.getFileName();
});
} finally {
try {
fs.close();
} catch (UnsupportedOperationException e) {
if (!isInstalled) {
throw new RuntimeException(
"UnsupportedOperationException is thrown unexpectedly");
}
}
}
}
示例11: clearDir
import java.nio.file.Files; //導入方法依賴的package包/類
private static boolean clearDir(Path path, Predicate<Path> disallowed) throws IOException {
try (Stream<Path> stream = Files.walk(path, FileVisitOption.FOLLOW_LINKS)) {
if (stream.anyMatch(disallowed)) return false;
}
AtomicBoolean ret = new AtomicBoolean(true);
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (disallowed.test(file)) {
ret.set(false);
return FileVisitResult.TERMINATE;
} else {
Files.delete(file);
return FileVisitResult.CONTINUE;
}
}
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
if (exc != null) throw exc;
if (!dir.equals(path)) Files.delete(dir);
return FileVisitResult.CONTINUE;
}
});
return ret.get();
}
示例12: getSourceFiles
import java.nio.file.Files; //導入方法依賴的package包/類
private List<SourceFile> getSourceFiles(Path basePath) throws IOException {
try (Stream<Path> stream = Files.walk(basePath)) {
return stream.filter(path -> path.getFileName().toString().endsWith(".java"))
.map(path -> new FileSystemSourceFile(basePath, basePath.relativize(path)))
.collect(Collectors.toList());
}
}
示例13: getChartTgzs
import java.nio.file.Files; //導入方法依賴的package包/類
List<String> getChartTgzs(String path) throws MojoExecutionException {
try (Stream<Path> files = Files.walk(Paths.get(path))) {
return files.filter(p -> p.getFileName().toString().endsWith("tgz"))
.map(p -> p.toString())
.collect(Collectors.toList());
} catch (IOException e) {
throw new MojoExecutionException("Unable to scan repo directory at " + path, e);
}
}
示例14: walk
import java.nio.file.Files; //導入方法依賴的package包/類
private Map<String, ModuleReference> walk(Path root) {
try (Stream<Path> stream = Files.walk(root, 1)) {
return stream.filter(path -> !path.equals(root))
.map(this::toModuleReference)
.collect(toMap(mref -> mref.descriptor().name(),
Function.identity()));
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
示例15: getMultipleChoiceImageResources
import java.nio.file.Files; //導入方法依賴的package包/類
public List<String> getMultipleChoiceImageResources(){
final URI uri;
final ArrayList<String> resourceList;
final PathMatcher matcher;
final Path myPath;
final Stream<Path> walk;
resourceList = new ArrayList<String>();
matcher = FileSystems.getDefault().getPathMatcher(GlobFactoryHelper.getCaseInsensitiveExtensionGlob("PNG","JPG"));
try {
uri = ResourceHelper.getResource(isFromGameResourceInput(), gamePath, setPath).toURI();
myPath= Paths.get(uri);
walk = Files.walk(myPath, 3, FileVisitOption.FOLLOW_LINKS);
for (Iterator<Path> it = walk.iterator(); it.hasNext();){
Path currentPath =it.next();
if (matcher.matches(currentPath))
{ resourceList.add(currentPath.toString().substring(currentPath.toString().indexOf("game-resources-input/")));
}
}
} catch (IOException | URISyntaxException e1) {
log.error("Fail",e1);
}
return resourceList;
/* final String pattern;
final String resourcePath;
final List<String> resourceList;
pattern=GlobFactoryHelper.getCaseInsensitiveExtensionGlob("PNG","JPG");
resourcePath= ResourceHelper.getResourceAddress( isFromGameResourceInput(),getGamePath(), getSetPath());
resourceList= ImageFilesLoaderHelper.getImageResources(pattern, resourcePath, 3, log);
return resourceList;
*/
}