本文整理汇总了Java中java.nio.file.Files.list方法的典型用法代码示例。如果您正苦于以下问题:Java Files.list方法的具体用法?Java Files.list怎么用?Java Files.list使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.Files
的用法示例。
在下文中一共展示了Files.list方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: importAll
import java.nio.file.Files; //导入方法依赖的package包/类
private static void importAll(Path parent, Importer importer, List<ReadOnlyDataSource> dataSources) throws IOException {
if (Files.isDirectory(parent)) {
try (Stream<Path> stream = Files.list(parent)) {
stream.sorted().forEach(child -> {
if (Files.isDirectory(child)) {
try {
importAll(child, importer, dataSources);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
addDataSource(parent, child, importer, dataSources);
}
});
}
} else {
if (parent.getParent() != null) {
addDataSource(parent.getParent(), parent, importer, dataSources);
}
}
}
示例2: getFileNameList
import java.nio.file.Files; //导入方法依赖的package包/类
public static List<String> getFileNameList(Path path)
throws IOException
{
ImmutableList<String> fileNames;
try (Stream<Path> stream = Files.list(path)) {
fileNames = stream.filter(Files::isRegularFile)
.map(Path::getFileName).map(Path::toString)
.collect(toImmutableList());
}
return fileNames;
}
示例3: testKnownDirectories
import java.nio.file.Files; //导入方法依赖的package包/类
@Test(dataProvider = "knownDirectories")
public void testKnownDirectories(String path, boolean theDefault) throws Exception {
if (isExplodedBuild && !theDefault) {
System.out.println("Skip testKnownDirectories with non-default FileSystem");
return;
}
FileSystem fs = selectFileSystem(theDefault);
Path dir = fs.getPath(path);
assertTrue(Files.isDirectory(dir));
// directory should not be empty
try (Stream<Path> stream = Files.list(dir)) {
assertTrue(stream.count() > 0L);
}
try (Stream<Path> stream = Files.walk(dir)) {
assertTrue(stream.count() > 0L);
}
}
示例4: fileCompletions
import java.nio.file.Files; //导入方法依赖的package包/类
private static CompletionProvider fileCompletions(Predicate<Path> accept) {
return (code, cursor, anchor) -> {
int lastSlash = code.lastIndexOf('/');
String path = code.substring(0, lastSlash + 1);
String prefix = lastSlash != (-1) ? code.substring(lastSlash + 1) : code;
Path current = toPathResolvingUserHome(path);
List<Suggestion> result = new ArrayList<>();
try (Stream<Path> dir = Files.list(current)) {
dir.filter(f -> accept.test(f) && f.getFileName().toString().startsWith(prefix))
.map(f -> new ArgSuggestion(f.getFileName() + (Files.isDirectory(f) ? "/" : "")))
.forEach(result::add);
} catch (IOException ex) {
//ignore...
}
if (path.isEmpty()) {
StreamSupport.stream(FileSystems.getDefault().getRootDirectories().spliterator(), false)
.filter(root -> Files.exists(root))
.filter(root -> accept.test(root) && root.toString().startsWith(prefix))
.map(root -> new ArgSuggestion(root.toString()))
.forEach(result::add);
}
anchor[0] = path.length();
return result;
};
}
示例5: testUserHome
import java.nio.file.Files; //导入方法依赖的package包/类
@Test
public void testUserHome() throws IOException {
List<String> completions;
Path home = Paths.get(System.getProperty("user.home"));
try (Stream<Path> content = Files.list(home)) {
completions = content.filter(CLASSPATH_FILTER)
.map(file -> file.getFileName().toString() + (Files.isDirectory(file) ? "/" : ""))
.sorted()
.collect(Collectors.toList());
}
testNoStartUp(
a -> assertCompletion(a, "/env --class-path ~/|", false, completions.toArray(new String[completions.size()]))
);
}
示例6: addDirectory
import java.nio.file.Files; //导入方法依赖的package包/类
private void addDirectory(Path dir, boolean warn) {
if (!Files.isDirectory(dir)) {
if (warn) {
log.warning(Lint.LintCategory.PATH,
Warnings.DirPathElementNotFound(dir));
}
return;
}
try (Stream<Path> s = Files.list(dir)) {
s.filter(Locations.this::isArchive)
.forEach(dirEntry -> addFile(dirEntry, warn));
} catch (IOException ignore) {
}
}
示例7: testSnapshotWithMissingShardLevelIndexFile
import java.nio.file.Files; //导入方法依赖的package包/类
public void testSnapshotWithMissingShardLevelIndexFile() throws Exception {
Path repo = randomRepoPath();
logger.info("--> creating repository at {}", repo.toAbsolutePath());
assertAcked(client().admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(
Settings.builder().put("location", repo).put("compress", false)));
createIndex("test-idx-1", "test-idx-2");
logger.info("--> indexing some data");
indexRandom(true,
client().prepareIndex("test-idx-1", "doc").setSource("foo", "bar"),
client().prepareIndex("test-idx-2", "doc").setSource("foo", "bar"));
logger.info("--> creating snapshot");
client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-1")
.setWaitForCompletion(true).setIndices("test-idx-*").get();
logger.info("--> deleting shard level index file");
try (Stream<Path> files = Files.list(repo.resolve("indices"))) {
files.forEach(indexPath ->
IOUtils.deleteFilesIgnoringExceptions(indexPath.resolve("0").resolve("index-0"))
);
}
logger.info("--> creating another snapshot");
CreateSnapshotResponse createSnapshotResponse =
client().admin().cluster().prepareCreateSnapshot("test-repo", "test-snap-2")
.setWaitForCompletion(true).setIndices("test-idx-1").get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
assertEquals(createSnapshotResponse.getSnapshotInfo().successfulShards(), createSnapshotResponse.getSnapshotInfo().totalShards());
logger.info("--> restoring the first snapshot, the repository should not have lost any shard data despite deleting index-N, " +
"because it should have iterated over the snap-*.data files as backup");
client().admin().indices().prepareDelete("test-idx-1", "test-idx-2").get();
RestoreSnapshotResponse restoreSnapshotResponse =
client().admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap-1").setWaitForCompletion(true).get();
assertEquals(0, restoreSnapshotResponse.getRestoreInfo().failedShards());
}
示例8: verifyFileMatching
import java.nio.file.Files; //导入方法依赖的package包/类
@Test
public void verifyFileMatching ()
throws Exception {
try (Stream<Path> pathStream = Files.list( new File( "/java" ).toPath() )) {
Optional<Path> deploymentCheck = pathStream
.filter( path -> path.getFileName().toString().endsWith( ".bat" ) )
.findFirst();
// logger.info( "object: {} : value: {}", deploymentCheck,
// deploymentCheck.get() );
}
}
示例9: listFiles
import java.nio.file.Files; //导入方法依赖的package包/类
private List<String> listFiles(Path path, Predicate<? super Path> filter) throws IOException {
try (Stream<Path> stream = Files.list(path)) {
return stream.filter(filter)
.map(p -> p.getFileName().toString() + (Files.isDirectory(p) ? "/" : ""))
.sorted()
.collect(Collectors.toList());
}
}
示例10: list
import java.nio.file.Files; //导入方法依赖的package包/类
private Stream<Path> list(Path source) {
try {
return Files.list(source);
} catch (IOException e) {
validator.handleIOException(e);
return null;
}
}
示例11: currentVersion
import java.nio.file.Files; //导入方法依赖的package包/类
private long currentVersion(StreamId streamId) {
try (Stream<Path> stream = Files.list(directory)) {
return stream
.filter(p -> p.getFileName().toString().endsWith(dataSuffix))
.filter(p -> FilenameCodec.parse(p, (timestamp, fileStreamId, eventNumber, eventType) -> fileStreamId.equals(streamId)))
.count() - 1;
} catch (IOException e) {
throw new RuntimeException("Unable to count files in " + directory);
}
}
示例12: currentGlobalVersion
import java.nio.file.Files; //导入方法依赖的package包/类
private long currentGlobalVersion() {
try (Stream<Path> stream = Files.list(directory)) {
return stream.filter(p -> p.getFileName().toString().endsWith(dataSuffix)).count();
} catch (IOException e) {
throw new RuntimeException("Unable to count files in " + directory);
}
}
示例13: streamExists
import java.nio.file.Files; //导入方法依赖的package包/类
boolean streamExists(StreamId streamId) {
try (Stream<Path> stream = Files.list(directory)) {
return stream
.filter(p -> p.getFileName().toString().endsWith(dataSuffix))
.anyMatch(p -> FilenameCodec.parse(p, (timestamp, fileStreamId, eventNumber, eventType) -> fileStreamId.equals(streamId)));
} catch (IOException e) {
throw new RuntimeException("Unable to count files in " + directory);
}
}
示例14: truncateStoreFiles
import java.nio.file.Files; //导入方法依赖的package包/类
private void truncateStoreFiles(String storeName) {
try {
Stream<Path> list = Files.list(new File(".").toPath());
Object[] paths = list.toArray();
for (Object path : paths) {
if (path.toString().contains(storeName)) {
Files.write((Path) path, new byte[0], StandardOpenOption.TRUNCATE_EXISTING);
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
示例15: findFirst
import java.nio.file.Files; //导入方法依赖的package包/类
static Path findFirst(Path basedir, String glob) throws IOException {
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + glob);
try (Stream<Path> stream = Files.list(basedir)) {
return stream
.filter(matcher::matches)
.findFirst()
.orElseThrow(() -> new FileNotFoundException(basedir + "/.../" + glob));
}
}