下面列出了java.nio.file.DirectoryIteratorException#getCause ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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();
}
}
@Test
public void testEmptyPackageDirectory(Path base) throws Exception {
Path src = base.resolve("src");
createSources(src);
// need an empty package directory, to check whether
// the behavior of subpackage and package
Path pkgdir = src.resolve("m1/m1pro/");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(pkgdir, "*.java")) {
for (Path entry : stream) {
Files.deleteIfExists(entry);
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration
throw ex.getCause();
}
execTask("--module-source-path", src.toString(),
"-subpackages", "m1/m1pro");
checkPackagesSpecified("m1pro", "m1pro.pro1", "m1pro.pro2");
// empty package directory should cause an error
execNegativeTask("--module-source-path", src.toString(),
"m1/m1pro");
}
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();
}
}
/**
* 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;
}
public static FileSelection create(final FileSystem fs, Path combined) throws IOException {
Stopwatch timer = Stopwatch.createStarted();
// NFS filesystems has delay before files written by executor shows up in the coordinator.
// For NFS, fs.exists() will force a refresh if the directory is not found
// No action is taken if it returns false as the code path already handles the Exception case
fs.exists(combined);
final ImmutableList<FileAttributes> fileAttributes;
try(DirectoryStream<FileAttributes> stream = FileSystemUtils.globRecursive(fs, combined, NO_HIDDEN_FILES)) {
fileAttributes = ImmutableList.copyOf(stream);
} catch (DirectoryIteratorException e) {
throw e.getCause();
}
logger.trace("Returned files are: {}", fileAttributes);
if (fileAttributes == null || fileAttributes.isEmpty()) {
return null;
}
final FileSelection fileSel = createFromExpanded(fileAttributes, combined.toURI().getPath());
logger.debug("FileSelection.create() took {} ms ", timer.elapsed(TimeUnit.MILLISECONDS));
return fileSel;
}
/**
* 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;
}
/**
* Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
* test directory.
*
* @throws IOException Thrown if the test directory could not be deleted
*/
@After
public void tearDown() throws IOException {
//Unset Mojo instance
mojo = null;
//Delete test directory
final Path testDir = Paths.get(TEST_DIR);
if (Files.exists(testDir)) {
//First get all files in the test directory (if the test directory
//exists and delete them. This is necessary because there is no
//method for recursivly deleting a directory in the Java API.
try (final DirectoryStream<Path> files = Files.newDirectoryStream(
testDir)) {
for (final Path file : files) {
Files.deleteIfExists(file);
}
} catch (DirectoryIteratorException ex) {
throw ex.getCause();
}
//Delete the (now empty) test directory.
Files.deleteIfExists(testDir);
}
}
/**
* Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
* test directory.
*
* @throws IOException Thrown if the test directory could not be deleted
*/
@After
public void tearDown() throws IOException {
//Unset Mojo instance
mojo = null;
//Delete test directory
final Path testDir = Paths.get(TEST_DIR);
if (Files.exists(testDir)) {
//First get all files in the test directory (if the test directory
//exists and delete them. This is necessary because there is no
//method for recursivly deleting a directory in the Java API.
try (final DirectoryStream<Path> files = Files.newDirectoryStream(
testDir)) {
for (final Path file : files) {
Files.deleteIfExists(file);
}
} catch (DirectoryIteratorException ex) {
throw ex.getCause();
}
//Delete the (now empty) test directory.
Files.deleteIfExists(testDir);
}
}
/**
* Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
* test directory.
*
* @throws IOException Thrown if the test directory could not be deleted
*/
@After
public void tearDown() throws IOException {
//Unset Mojo instance
mojo = null;
//Delete test directory
final Path testDir = Paths.get(TEST_DIR);
if (Files.exists(testDir)) {
//First get all files in the test directory (if the test directory
//exists and delete them. This is necessary because there is no
//method for recursivly deleting a directory in the Java API.
try (final DirectoryStream<Path> files = Files.newDirectoryStream(
testDir)) {
for (final Path file : files) {
Files.deleteIfExists(file);
}
} catch (DirectoryIteratorException ex) {
throw ex.getCause();
}
//Delete the (now empty) test directory.
Files.deleteIfExists(testDir);
}
}
/**
* Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
* test directory.
*
* @throws IOException Thrown if the test directory could not be deleted
*/
@After
public void tearDown() throws IOException {
//Unset Mojo instance
mojo = null;
//Delete test directory
final Path testDir = Paths.get(TEST_DIR);
if (Files.exists(testDir)) {
//First get all files in the test directory (if the test directory
//exists and delete them. This is necessary because there is no
//method for recursivly deleting a directory in the Java API.
try (final DirectoryStream<Path> files = Files.newDirectoryStream(
testDir)) {
for (final Path file : files) {
Files.deleteIfExists(file);
}
} catch (DirectoryIteratorException ex) {
throw ex.getCause();
}
//Delete the (now empty) test directory.
Files.deleteIfExists(testDir);
}
}
/**
* Resets the Mojo by setting {@link #mojo} to {@code null} and deletes the
* test directory.
*
* @throws IOException Thrown if the test directory could not be deleted
*/
@After
public void tearDown() throws IOException {
//Unset Mojo instance
mojo = null;
//Delete test directory
final Path testDir = Paths.get(TEST_DIR);
if (Files.exists(testDir)) {
//First get all files in the test directory (if the test directory
//exists and delete them. This is necessary because there is no
//method for recursivly deleting a directory in the Java API.
try (final DirectoryStream<Path> files = Files.newDirectoryStream(
testDir)) {
for (final Path file : files) {
Files.deleteIfExists(file);
}
} catch (DirectoryIteratorException ex) {
throw ex.getCause();
}
//Delete the (now empty) test directory.
Files.deleteIfExists(testDir);
}
}
@Override
public Set<AbstractDirectory> getDirectories() {
Set<Path> result = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(representedPath)) {
for (Path entry : stream) {
if (Files.isDirectory(entry)) {
result.add(entry);
}
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw new UncheckedIOException((IOException) ex.getCause());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return result.stream().map(FileSystemDirectory::new).collect(Collectors.toSet());
}
@Override
protected Set<AbstractFile> getFilesNotConsiderPatterns() {
Set<Path> result = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(representedPath)) {
for (Path entry : stream) {
if (Files.isRegularFile(entry)) {
result.add(entry);
}
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw new UncheckedIOException((IOException) ex.getCause());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return result.stream().map(FileSystemFile::new).collect(Collectors.toSet());
}
private static List<Path> listResFiles(Path dir, String pattern) throws IOException {
List<Path> result = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, pattern)) {
for (Path entry : stream) {
result.add(entry);
}
} catch (DirectoryIteratorException ex) {
throw ex.getCause();
}
return result;
}
/**
* Count the number of entries in a directory.
*
* @param dir directory to evaluate
* @return number of inodes under it.
* @throws IOException
*/
@Nonnegative
public static int count(@Nonnull final Path dir) throws IOException {
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
return Iterables.size(stream);
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw ex.getCause();
}
}
/**
* Convenience method to return all paths in a SMALL directory.
* <p>
* DO NOT USE THIS TO TRAVERSE LARGE (multi-thousand inode) DIRECTORIES!
* <p>
* For starters you shouldn't be making directories that big at all, but if you did please use
* {@link Files#newDirectoryStream(Path)} directly in your code.
*
* @param dir directory to evaluate
* @return all files in that directory
* @throws IOException
*/
@Nonnull
public static List<Path> list(@Nonnull final Path dir) throws IOException {
final List<Path> contents = new ArrayList<>();
try (final DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (final Path entry : stream) {
contents.add(entry);
}
} catch (DirectoryIteratorException ex) {
// I/O error encounted during the iteration, the cause is an IOException
throw ex.getCause();
}
return contents;
}
public static void createFileList(String target, List<Path> files) throws IOException
{
if (target == null)
return;
Path root = Paths.get(target);
if (Files.exists(root) == false)
throw new IOException("Cannot access input file '"+root+"'");
if ((Files.isRegularFile(root) == true) && (Files.isHidden(root) == true))
throw new IOException("Cannot access input file '"+root+"'");
if (Files.isRegularFile(root) == true)
{
if (target.charAt(0) != '.')
files.add(root);
return;
}
// If not a regular file and not a directory (a link ?), fail
if (Files.isDirectory(root) == false)
throw new IOException("Invalid file type '"+root+"'");
String suffix = File.separator + ".";
String strRoot = root.toString();
boolean isRecursive = !strRoot.endsWith(suffix);
if (isRecursive == true)
{
if (strRoot.endsWith(File.separator) == false)
root = Paths.get(strRoot+File.separator);
}
else
{
// Remove suffix
root = Paths.get(strRoot.substring(0, strRoot.length()-1));
}
try (DirectoryStream<Path> stream = Files.newDirectoryStream(root))
{
for (Path entry: stream)
{
if ((Files.exists(entry) == false) || (Files.isHidden(entry) == true))
continue;
if ((Files.isRegularFile(entry) == true) && (String.valueOf(entry.getFileName()).startsWith(".") == false))
files.add(entry);
else if ((isRecursive == true) && (Files.isDirectory(entry) == true))
createFileList(entry.toString(), files);
}
}
catch (DirectoryIteratorException e)
{
throw e.getCause();
}
}