本文整理匯總了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));
}
}