下面列出了java.nio.file.DirectoryStream#iterator ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static List<SourcePath> doScan(File root, File dir) {
List<SourcePath> sourcePaths = new ArrayList<>();
DirectoryStream<Path> dirStream = null;
try {
dirStream = Files.newDirectoryStream(dir.toPath());
Iterator<Path> it = dirStream.iterator();
while (it.hasNext()) {
Path next = it.next();
if (Files.isDirectory(next)) {
sourcePaths.addAll(doScan(root, next.toFile()));
} else {
sourcePaths.add(new SourcePath(root, next.toFile()));
}
}
} catch (IOException ex) {
if (dirStream != null) {
try {
dirStream.close();
} catch (IOException e) { }
}
}
return sort(sourcePaths);
}
private void upgradeData() {
logger.info("Migrating data to new storage format");
long startTime = System.currentTimeMillis();
try {
DirectoryStream<Path> ds = Files.newDirectoryStream(FileSystems.getDefault().getPath(BSql.OLD_BSQL_BASE_FOLDER));
Iterator<Path> iterator = ds.iterator();
while (iterator.hasNext()) {
Path path = iterator.next();
if (Files.isDirectory(path)) {
portApplication(path.getFileName().toString());
}
}
} catch (IOException ex) {
java.util.logging.Logger.getLogger(Version1to2.class.getName()).log(Level.SEVERE, null, ex);
}
long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Data migration to new storage format successfully completed in " + elapsedTime + "ms");
}
/**
* According to Java documentation of NIO API, if we try to call
* <code>next</code> on iterator of closed directory stream, there should be
* an {@see NoSuchElementException} exception.
*
* @throws IOException
* if we can't get the DirectoryStream
*/
@Test(expected = NoSuchElementException.class)
public void testNextOnClosedDirectoryStreamIterator() throws IOException {
Path dir = Paths.get(clusterUri);
// create file
Path file = dir.resolve("foo");
if (Files.exists(file)) {
Files.delete(file);
}
Files.createFile(file);
DirectoryStream<Path> stream = Files.newDirectoryStream(dir);
Iterator<Path> it = stream.iterator();
stream.close();
Assert.assertFalse(it.hasNext());
// This one throws the exception
it.next();
}
private Iterator<Path> getUncompressedLogrotatedFileIterator(
SingularityExecutorTaskDefinition taskDefinition
) {
final Path serviceLogOutPath = taskDefinition.getServiceLogOutPath();
final Path parent = serviceLogOutPath.getParent();
if (parent == null) {
throw new IllegalStateException(
"Service log path " + serviceLogOutPath + " has no parent"
);
}
final Path logrotateToPath = parent.resolve(
executorConfiguration.getLogrotateToDirectory()
);
if (!logrotateToPath.toFile().exists() || !logrotateToPath.toFile().isDirectory()) {
LOG.warn(
"Skipping uncompressed logrotated file cleanup for {} -- {} does not exist or is not a directory (task sandbox was probably garbage collected by Mesos)",
taskDefinition.getTaskId(),
logrotateToPath
);
return Collections.emptyIterator();
}
try {
DirectoryStream<Path> dirStream = Files.newDirectoryStream(
logrotateToPath,
String.format("%s-*", serviceLogOutPath.getFileName())
);
return dirStream.iterator();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public Iterator<String> cardinality(String app, String table, String column) throws OperationException {
Path path = Paths.get(PathUtil.indexColumnFolder(app, table, column));
Iterator<String> iterator;
try {
DirectoryStream directoryStream = Files.newDirectoryStream(path);
iterator = new Iterator<String>() {
Iterator<Path> innerIterator = directoryStream.iterator();
@Override
public boolean hasNext() {
boolean hasNext = innerIterator.hasNext();
if (!hasNext) {
try {
directoryStream.close();
} catch (IOException ex) {
Logger.getLogger(OnDiskHashedIndex.class.getName()).log(Level.SEVERE, null, ex);
}
}
return hasNext;
}
@Override
public String next() {
return innerIterator.next().getFileName().toString();
}
@Override
public void remove() {
innerIterator.remove();
}
};
} catch (IOException ex) {
return null;
}
return iterator;
}
private static Path findJFRFileInCurrentDirectory() {
try {
DirectoryStream<Path> ds = Files.newDirectoryStream(Paths.get("."), "*pid-*.jfr");
Iterator<Path> pathIterator = ds.iterator();
if (!pathIterator.hasNext()) {
throw new RuntimeException("Could not find jfr file in current directory");
}
return pathIterator.next();
} catch (IOException e) {
throw new RuntimeException("Could not list jfr file in current directory");
}
}
/**
* Deletes any contents that the directory might have, both files and folders; the directory
* itself is not deleted
*
* @param directoryName the target directory name
* @throws FileSystemOperationException if the parameter is not a directory or does not exist
*/
private void purgeContents(
String directoryName ) {
File file = new File(directoryName);
boolean exists = checkFileExistence(file, false);
if (exists) {
if (file.isDirectory()) {
DirectoryStream<Path> dirStream = null;
try {
dirStream = Files.newDirectoryStream(file.toPath());
Iterator<Path> it = dirStream.iterator();
while (it.hasNext()) {
Path path = it.next();
deleteRecursively(path.toFile());
}
} catch (Exception e) {
throw new FileSystemOperationException("Could not purge contents of directory '" + directoryName
+ "'", e);
} finally {
IoUtils.closeStream(dirStream);
}
} else {
throw new FileSystemOperationException(directoryName + " is not a directory! ");
}
}
}
@Test
public void testDirectoryStreamEmptyDir() throws IOException {
final String root = UUID.randomUUID().toString();
Path rootPath = Paths.get( filesystemPath, root );
// create test dir
assertFalse( Files.exists( rootPath ) );
Files.createDirectories( rootPath );
assertTrue( Files.exists( rootPath ) );
assertTrue( Files.isDirectory( rootPath ) );
// test dirstream
DirectoryStream<Path> ds = Files.newDirectoryStream(
FileSystems.getFileSystem( uri ).getPath( root ) );
try {
Iterator<Path> iter = ds.iterator();
assertFalse( iter.hasNext() );
try {
iter.next();
fail( "expected an exception" );
}
catch ( NoSuchElementException e ) {
// pass
}
}
finally {
ds.close();
}
}
/**
*
*/
protected ArchiveDirectoryStream(DirectoryStream<Input> parent, String glob) {
this.parentStream = parent;
this.parent = parent.iterator();
if (glob == null) {
glob = "";
}
this.glob = glob;
boolean containsWildcard = (glob.contains("?") || glob.contains("*"));
pattern = containsWildcard ? Pattern.compile(glob.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\.", "\\\\.").replaceAll("\\?", "\\.").replaceAll("\\*", ".*")) : null;
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
SimpleDirectoryIterator(MCRPath dir, DirectoryStream<Path> baseStream) {
this.baseIterator = baseStream.iterator();
this.dir = dir;
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
/**
* @param parent
*/
public CompoundDirectoryStream(DirectoryStream<DirectoryStream<T>> parent) {
this.parent = parent;
this.streamIterator = parent.iterator();
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}
private DirectoryStream<Path> wrap(final DirectoryStream<Path> stream) {
return new DirectoryStream<Path>() {
@Override
public Iterator<Path> iterator() {
final Iterator<Path> itr = stream.iterator();
return new Iterator<Path>() {
private Path next = null;
@Override
public boolean hasNext() {
if (next == null) {
if (itr.hasNext()) {
next = itr.next();
} else {
return false;
}
}
if (next != null) {
try {
triggerEx(next, "DirectoryIteratorException");
} catch (IOException ioe) {
throw new DirectoryIteratorException(ioe);
} catch (SecurityException se) {
// ??? Does DS throw SecurityException during iteration?
next = null;
return hasNext();
}
}
return (next != null);
}
@Override
public Path next() {
try {
if (next != null || hasNext()) {
return new PassThroughFileSystem.PassThroughPath(delegate, next);
} else {
throw new NoSuchElementException();
}
} finally {
next = null;
}
}
@Override
public void remove() {
itr.remove();
}
};
}
@Override
public void close() throws IOException {
stream.close();
}
};
}