本文整理汇总了Java中java.nio.file.DirectoryStream类的典型用法代码示例。如果您正苦于以下问题:Java DirectoryStream类的具体用法?Java DirectoryStream怎么用?Java DirectoryStream使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
DirectoryStream类属于java.nio.file包,在下文中一共展示了DirectoryStream类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: z2zmove
import java.nio.file.DirectoryStream; //导入依赖的package包/类
private static void z2zmove(FileSystem src, FileSystem dst, String path)
throws IOException
{
Path srcPath = src.getPath(path);
Path dstPath = dst.getPath(path);
if (Files.isDirectory(srcPath)) {
if (!Files.exists(dstPath))
mkdirs(dstPath);
try (DirectoryStream<Path> ds = Files.newDirectoryStream(srcPath)) {
for (Path child : ds) {
z2zmove(src, dst,
path + (path.endsWith("/")?"":"/") + child.getFileName());
}
}
} else {
//System.out.println("moving..." + path);
Path parent = dstPath.getParent();
if (parent != null && Files.notExists(parent))
mkdirs(parent);
Files.move(srcPath, dstPath);
}
}
示例2: children
import java.nio.file.DirectoryStream; //导入依赖的package包/类
@Override
public String[] children(String f) {
Path p = getPath(f);
try (DirectoryStream<Path> dir = Files.newDirectoryStream(p)){
java.util.List<String> result = new ArrayList<>();
for (Path child : dir) {
String name = child.getName(child.getNameCount() - 1).toString();
if (name.endsWith("/"))
name = name.substring(0, name.length() - 1);
result.add(name);
}
return result.toArray(new String[result.size()]);
} catch (IOException ex) {
return new String[0]; //huh?
}
}
示例3: deleteHeapTempFiles
import java.nio.file.DirectoryStream; //导入依赖的package包/类
public static void deleteHeapTempFiles() {
if (Platform.isWindows()) { // this is workaroud for JDK bug #6359560
try {
File tempDir = new File(System.getProperty("java.io.tmpdir")); // NOI18N
DirectoryStream<Path> files = Files.newDirectoryStream(tempDir.toPath());
try {
for (Path p : files) {
String fname = p.toFile().getName();
if (fname.startsWith("NBProfiler") && (fname.endsWith(".map") || fname.endsWith(".ref") || fname.endsWith(".gc"))) { // NOI18N
Files.delete(p);
}
}
} finally {
files.close();
}
} catch (IOException ex) {
System.err.println("deleteHeapTempFiles failed"); // NOI18N
ex.printStackTrace();
}
}
}
示例4: checkDirs
import java.nio.file.DirectoryStream; //导入依赖的package包/类
/**
* Recurse down a directory tree, checking all child directories.
* @param dir
* @throws DiskErrorException
*/
public static void checkDirs(File dir) throws DiskErrorException {
checkDir(dir);
IOException ex = null;
try (DirectoryStream<java.nio.file.Path> stream =
Files.newDirectoryStream(dir.toPath())) {
for (java.nio.file.Path entry: stream) {
File child = entry.toFile();
if (child.isDirectory()) {
checkDirs(child);
}
}
} catch (DirectoryIteratorException de) {
ex = de.getCause();
} catch (IOException ie) {
ex = ie;
}
if (ex != null) {
throw new DiskErrorException("I/O error when open a directory: "
+ dir.toString(), ex);
}
}
示例5: listDirectory
import java.nio.file.DirectoryStream; //导入依赖的package包/类
/**
* Return the complete list of files in a directory as strings.<p/>
*
* This is better than File#listDir because it does not ignore IOExceptions.
*
* @param dir The directory to list.
* @param filter If non-null, the filter to use when listing
* this directory.
* @return The list of files in the directory.
*
* @throws IOException On I/O error
*/
public static List<String> listDirectory(File dir, FilenameFilter filter)
throws IOException {
ArrayList<String> list = new ArrayList<String> ();
try (DirectoryStream<Path> stream =
Files.newDirectoryStream(dir.toPath())) {
for (Path entry: stream) {
String fileName = entry.getFileName().toString();
if ((filter == null) || filter.accept(dir, fileName)) {
list.add(fileName);
}
}
} catch (DirectoryIteratorException e) {
throw e.getCause();
}
return list;
}
示例6: scanAndLoadDictionaries
import java.nio.file.DirectoryStream; //导入依赖的package包/类
/**
* Scans the hunspell directory and loads all found dictionaries
*/
private void scanAndLoadDictionaries() throws IOException {
if (Files.isDirectory(hunspellDir)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(hunspellDir)) {
for (Path file : stream) {
if (Files.isDirectory(file)) {
try (DirectoryStream<Path> inner = Files.newDirectoryStream(hunspellDir.resolve(file), "*.dic")) {
if (inner.iterator().hasNext()) { // just making sure it's indeed a dictionary dir
try {
dictionaries.getUnchecked(file.getFileName().toString());
} catch (UncheckedExecutionException e) {
// The cache loader throws unchecked exception (see #loadDictionary()),
// here we simply report the exception and continue loading the dictionaries
logger.error("exception while loading dictionary {}", file.getFileName(), e);
}
}
}
}
}
}
}
}
示例7: getClassPaths
import java.nio.file.DirectoryStream; //导入依赖的package包/类
private List<Path> getClassPaths(String cpaths) {
if (cpaths.isEmpty()) {
return Collections.emptyList();
}
List<Path> paths = new ArrayList<>();
for (String p : cpaths.split(File.pathSeparator)) {
if (p.length() > 0) {
// wildcard to parse all JAR files e.g. -classpath dir/*
int i = p.lastIndexOf(".*");
if (i > 0) {
Path dir = Paths.get(p.substring(0, i));
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.jar")) {
for (Path entry : stream) {
paths.add(entry);
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
} else {
paths.add(Paths.get(p));
}
}
}
return paths;
}
示例8: doNothingWhenPathCannotBeParsed
import java.nio.file.DirectoryStream; //导入依赖的package包/类
@Test
@SuppressWarnings("unchecked")
public void doNothingWhenPathCannotBeParsed() throws Exception {
mockStatic(Files.class);
when(validator.isValid(path)).thenReturn(true);
DirectoryStream<Path> directoryStream = mock(DirectoryStream.class);
when(Files.newDirectoryStream(path, "*.xml")).thenReturn(directoryStream);
Iterator<Path> iterator = mock(Iterator.class);
when(directoryStream.iterator()).thenReturn(iterator);
when(iterator.hasNext()).thenReturn(true, false);
Path file = mock(Path.class);
when(iterator.next()).thenReturn(file);
when(xmlParser.parseXml(file)).thenReturn(Optional.empty());
scheduledFileReader.parseXmlFiles();
verifyZeroInteractions(service);
verifyStatic(Files.class);
Files.newDirectoryStream(path, "*.xml");
}
示例9: listBlobsByPrefix
import java.nio.file.DirectoryStream; //导入依赖的package包/类
@Override
public ImmutableMap<String, BlobMetaData> listBlobsByPrefix(String blobNamePrefix) throws IOException {
// using MapBuilder and not ImmutableMap.Builder as it seems like File#listFiles might return duplicate files!
MapBuilder<String, BlobMetaData> builder = MapBuilder.newMapBuilder();
blobNamePrefix = blobNamePrefix == null ? "" : blobNamePrefix;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, blobNamePrefix + "*")) {
for (Path file : stream) {
final BasicFileAttributes attrs = Files.readAttributes(file, BasicFileAttributes.class);
if (attrs.isRegularFile()) {
builder.put(file.getFileName().toString(), new PlainBlobMetaData(file.getFileName().toString(), attrs.size()));
}
}
}
return builder.immutableMap();
}
示例10: findJsonSpec
import java.nio.file.DirectoryStream; //导入依赖的package包/类
/**
* Returns the json files found within the directory provided as argument.
* Files are looked up in the classpath, or optionally from {@code fileSystem} if its not null.
*/
public static Set<Path> findJsonSpec(FileSystem fileSystem, String optionalPathPrefix, String path) throws IOException {
Path dir = resolveFile(fileSystem, optionalPathPrefix, path, null);
if (!Files.isDirectory(dir)) {
throw new NotDirectoryException(path);
}
Set<Path> jsonFiles = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path item : stream) {
if (item.toString().endsWith(JSON_SUFFIX)) {
jsonFiles.add(item);
}
}
}
if (jsonFiles.isEmpty()) {
throw new NoSuchFileException(path, null, "no json files found");
}
return jsonFiles;
}
示例11: getChildren
import java.nio.file.DirectoryStream; //导入依赖的package包/类
@Override
public List<Node> getChildren() {
if (!isDirectory())
throw new IllegalArgumentException("not a directory: " + getNameString());
if (children == null) {
List<Node> list = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
for (Path p : stream) {
p = explodedModulesDir.relativize(p);
String pName = MODULES + nativeSlashToFrontSlash(p.toString());
Node node = findNode(pName);
if (node != null) { // findNode may choose to hide certain files!
list.add(node);
}
}
} catch (IOException x) {
return null;
}
children = list;
}
return children;
}
示例12: availableIndexFolders
import java.nio.file.DirectoryStream; //导入依赖的package包/类
/**
* Returns all folder names in ${data.paths}/nodes/{node.id}/indices folder
*/
public Set<String> availableIndexFolders() throws IOException {
if (nodePaths == null || locks == null) {
throw new IllegalStateException("node is not configured to store local location");
}
assertEnvIsLocked();
Set<String> indexFolders = new HashSet<>();
for (NodePath nodePath : nodePaths) {
Path indicesLocation = nodePath.indicesPath;
if (Files.isDirectory(indicesLocation)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(indicesLocation)) {
for (Path index : stream) {
if (Files.isDirectory(index)) {
indexFolders.add(index.getFileName().toString());
}
}
}
}
}
return indexFolders;
}
示例13: cleanupOldFiles
import java.nio.file.DirectoryStream; //导入依赖的package包/类
private void cleanupOldFiles(final String prefix, final String currentStateFile, Path[] locations) throws IOException {
final DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
final String entryFileName = entry.getFileName().toString();
return Files.isRegularFile(entry)
&& entryFileName.startsWith(prefix) // only state files
&& currentStateFile.equals(entryFileName) == false; // keep the current state file around
}
};
// now clean up the old files
for (Path dataLocation : locations) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dataLocation.resolve(STATE_DIR_NAME), filter)) {
for (Path stateFile : stream) {
Files.deleteIfExists(stateFile);
}
}
}
}
示例14: findMaxStateId
import java.nio.file.DirectoryStream; //导入依赖的package包/类
long findMaxStateId(final String prefix, Path... locations) throws IOException {
long maxId = -1;
for (Path dataLocation : locations) {
final Path resolve = dataLocation.resolve(STATE_DIR_NAME);
if (Files.exists(resolve)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(resolve, prefix + "*")) {
for (Path stateFile : stream) {
final Matcher matcher = stateFilePattern.matcher(stateFile.getFileName().toString());
if (matcher.matches()) {
final long id = Long.parseLong(matcher.group(1));
maxId = Math.max(maxId, id);
}
}
}
}
}
return maxId;
}
示例15: PollingWatchKey
import java.nio.file.DirectoryStream; //导入依赖的package包/类
PollingWatchKey(Path dir, PollingWatchService watcher, Object fileKey)
throws IOException
{
super(dir, watcher);
this.fileKey = fileKey;
this.valid = true;
this.tickCount = 0;
this.entries = new HashMap<Path,CacheEntry>();
// get the initial entries in the directory
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry: stream) {
// don't follow links
long lastModified =
Files.getLastModifiedTime(entry, LinkOption.NOFOLLOW_LINKS).toMillis();
entries.put(entry.getFileName(), new CacheEntry(lastModified, tickCount));
}
} catch (DirectoryIteratorException e) {
throw e.getCause();
}
}