本文整理汇总了Java中java.nio.file.spi.FileSystemProvider.newDirectoryStream方法的典型用法代码示例。如果您正苦于以下问题:Java FileSystemProvider.newDirectoryStream方法的具体用法?Java FileSystemProvider.newDirectoryStream怎么用?Java FileSystemProvider.newDirectoryStream使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.file.spi.FileSystemProvider
的用法示例。
在下文中一共展示了FileSystemProvider.newDirectoryStream方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import java.nio.file.spi.FileSystemProvider; //导入方法依赖的package包/类
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception{
Map<String, String> env = new HashMap<>();
JFileChooser jZipFileOpen = new JFileChooser("D:\\");
jZipFileOpen.setFileFilter(new FileNameExtensionFilter("Zip Files (\"*.zip, *.jar, *.syp\")",
"zip","jar","syp"));
if(jZipFileOpen.showOpenDialog(null)==JFileChooser.APPROVE_OPTION){
URI p = new URI("jar:" + jZipFileOpen.getSelectedFile().toURI().toString());
try (FileSystem zipfs = FileSystems.newFileSystem(p, env)){
FileSystemProvider provider = zipfs.provider();
Path dir = zipfs.getPath(zipfs.getSeparator());
try (DirectoryStream<Path> stream = provider.newDirectoryStream(dir,null)){
System.out.printf("%-20s\t%-20s\t%-20s\t%-20s%n%n",
"Filename","Create",
"Last Accessed","Last Modified");
for (Path file : stream) {
BasicFileAttributes bAttr = provider.getFileAttributeView(
file,BasicFileAttributeView.class)
.readAttributes();
String fname = file.getFileName().toString();
if(fname.length()>24){
fname = fname.substring(0, 19) + "...";
}
System.out.printf("%-24s%s\t%s\t%s%n",fname,
formatFileTime(bAttr.creationTime()),
formatFileTime(bAttr.lastAccessTime()),
formatFileTime(bAttr.lastModifiedTime()));
}
} catch (IOException | DirectoryIteratorException ex) {
ex.printStackTrace();
}
}
}
}
示例2: iterateOverDirectoryContents
import java.nio.file.spi.FileSystemProvider; //导入方法依赖的package包/类
private static boolean iterateOverDirectoryContents(FileSystem fileSystem, DirectoryIterationPaths dirIterationPaths,
Filter<Path> pathFilter, boolean recursiveListing, Function<DirectoryIterationPaths,Boolean> directoryContentAction) {
Path path = dirIterationPaths.getResultPath();
boolean printRecursive;
FileSystemProvider provider = fileSystem.provider();
DirectoryStream<Path> dirStream;
// Directory listing
try {
if (recursiveListing && provider instanceof CloudFileSystemProvider) {
// The CloudFileSystemProvider type is capable of providing a recursive listing,
// use this for efficiency if possible
LOG.debug("Using optimised cloud file system provider for recursive file listing");
dirStream = ((CloudFileSystemProvider)provider).newDirectoryStream(path, pathFilter, true);
printRecursive = false;
} else {
// Fallback to using recursive printing of the file listing when a directory is encountered
dirStream = provider.newDirectoryStream(path, pathFilter);
printRecursive = recursiveListing;
}
} catch (Exception e) {
LOG.error("Could not list directories for path '{}'", path, e);
return false;
}
// For each result in the dir
Iterator<Path> paths = dirStream.iterator();
while (paths.hasNext()) {
Path nextPath = paths.next();
DirectoryIterationPaths nextDirIterationPaths =
new DirectoryIterationPaths(dirIterationPaths, nextPath);
LOG.debug("Iterating through next path {}", nextDirIterationPaths);
// Can throw CancelException to terminate recursion/listing
if (!directoryContentAction.apply(nextDirIterationPaths)) {
return false;
}
// Recurse through subdirs as required
if (printRecursive && Files.isDirectory(nextPath)) {
if (!iterateOverDirectoryContents(fileSystem, nextDirIterationPaths, pathFilter,
printRecursive, directoryContentAction)) {
return false;
}
}
}
return true;
}