當前位置: 首頁>>代碼示例>>Java>>正文


Java FileSystemProvider.newDirectoryStream方法代碼示例

本文整理匯總了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();
            }
        }            
    } 
}
 
開發者ID:rlvillacarlos,項目名稱:cosc111,代碼行數:38,代碼來源:ZipView.java

示例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;
}
 
開發者ID:brdara,項目名稱:java-cloud-filesystem-provider,代碼行數:51,代碼來源:FileSystemProviderHelper.java


注:本文中的java.nio.file.spi.FileSystemProvider.newDirectoryStream方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。