Example usage for java.nio.file DirectoryIteratorException getCause

List of usage examples for java.nio.file DirectoryIteratorException getCause

Introduction

In this page you can find the example usage for java.nio.file DirectoryIteratorException getCause.

Prototype

@Override
public IOException getCause() 

Source Link

Document

Returns the cause of this exception.

Usage

From source file:com.buaa.cfs.utils.IOUtils.java

/**
 * Return the complete list of files in a directory as strings.<p/>
 * <p>//  ww  w  . ja v  a2s.  com
 * 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;
}

From source file:de.phillme.PhotoSorter.java

private List<PhotoFile> listSourceFiles() throws IOException, ImageProcessingException {
    List<PhotoFile> result = new ArrayList<>();
    Date date;// w ww .  j  av a2s.  c o m

    try (DirectoryStream<Path> stream = Files.newDirectoryStream(this.photosPath, "*.*")) {
        for (Path entry : stream) {
            date = null;
            PhotoFile photoFile;

            String fileType = detectMimeType(entry);
            //String fileExt = getFileExt(entry.getFileName().toString());

            if (fileType != null && fileType.contains("image")) {

                date = getDateFromExif(entry);
            }
            if (date != null) {
                photoFile = new PhotoFile(entry, date);

                result.add(photoFile);
            } else {
                LOGGER.info("Date of " + entry.getFileName()
                        + " could not be determined. Skipping for image processing...");
            }
        }
    } catch (DirectoryIteratorException ex) {
        // I/O error encounted during the iteration, the cause is an IOException
        throw ex.getCause();
    }
    return result;
}

From source file:org.codice.ddf.catalog.content.impl.FileSystemStorageProvider.java

private List<Path> listPaths(Path dir) throws IOException {
    List<Path> result = new ArrayList<>();
    if (Files.exists(dir)) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
            for (Path entry : stream) {
                result.add(entry);//  w w w .  j a  v  a  2  s.  co  m
            }
        } catch (DirectoryIteratorException ex) {
            // I/O error encounted during the iteration, the cause is an IOException
            throw ex.getCause();
        }
    }
    return result;
}

From source file:org.structr.web.maintenance.DirectFileImportCommand.java

@Override
public void execute(final Map<String, Object> attributes) throws FrameworkException {

    indexer = StructrApp.getInstance(securityContext).getFulltextIndexer();

    final String sourcePath = getParameterValueAsString(attributes, "source", null);
    final String modeString = getParameterValueAsString(attributes, "mode", Mode.COPY.name()).toUpperCase();
    final String existingString = getParameterValueAsString(attributes, "existing", Existing.SKIP.name())
            .toUpperCase();/* www .ja  v  a  2 s.  c om*/
    final boolean doIndex = Boolean
            .parseBoolean(getParameterValueAsString(attributes, "index", Boolean.TRUE.toString()));

    if (StringUtils.isBlank(sourcePath)) {
        throw new FrameworkException(422,
                "Please provide 'source' attribute for deployment source directory path.");
    }

    if (!EnumUtils.isValidEnum(Mode.class, modeString)) {
        throw new FrameworkException(422, "Unknown value for 'mode' attribute. Valid values are: copy, move");
    }

    if (!EnumUtils.isValidEnum(Existing.class, existingString)) {
        throw new FrameworkException(422,
                "Unknown value for 'existing' attribute. Valid values are: skip, overwrite, rename");
    }

    // use actual enums
    final Existing existing = Existing.valueOf(existingString);
    final Mode mode = Mode.valueOf(modeString);

    final List<Path> paths = new ArrayList<>();

    if (sourcePath.contains(PathHelper.PATH_SEP)) {

        final String folderPart = PathHelper.getFolderPath(sourcePath);
        final String namePart = PathHelper.getName(sourcePath);

        if (StringUtils.isNotBlank(folderPart)) {

            final Path source = Paths.get(folderPart);
            if (!Files.exists(source)) {

                throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
            }

            if (!Files.isDirectory(source)) {

                throw new FrameworkException(422, "Source path " + sourcePath + " is not a directory.");
            }

            try {

                try (final DirectoryStream<Path> stream = Files.newDirectoryStream(source, namePart)) {

                    for (final Path entry : stream) {
                        paths.add(entry);
                    }

                } catch (final DirectoryIteratorException ex) {
                    throw ex.getCause();
                }

            } catch (final IOException ioex) {
                throw new FrameworkException(422, "Unable to parse source path " + sourcePath + ".");
            }
        }

    } else {

        // Relative path
        final Path source = Paths.get(Settings.BasePath.getValue()).resolve(sourcePath);
        if (!Files.exists(source)) {

            throw new FrameworkException(422, "Source path " + sourcePath + " does not exist.");
        }

        paths.add(source);

    }

    final SecurityContext ctx = SecurityContext.getSuperUserInstance();
    final App app = StructrApp.getInstance(ctx);
    String targetPath = getParameterValueAsString(attributes, "target", "/");
    Folder targetFolder = null;

    ctx.setDoTransactionNotifications(false);

    if (StringUtils.isNotBlank(targetPath) && !("/".equals(targetPath))) {

        try (final Tx tx = app.tx()) {

            targetFolder = app.nodeQuery(Folder.class).and(StructrApp.key(Folder.class, "path"), targetPath)
                    .getFirst();
            if (targetFolder == null) {

                throw new FrameworkException(422, "Target path " + targetPath + " does not exist.");
            }

            tx.success();
        }
    }

    String msg = "Starting direct file import from source directory " + sourcePath + " into target path "
            + targetPath;
    logger.info(msg);
    publishProgressMessage(msg);

    paths.forEach((path) -> {

        try {

            final String newTargetPath;

            // If path is a directory, create it and use it as the new target folder
            if (Files.isDirectory(path)) {

                Path parentPath = path.getParent();
                if (parentPath == null) {
                    parentPath = path;
                }

                createFileOrFolder(ctx, app, parentPath, path,
                        Files.readAttributes(path, BasicFileAttributes.class), sourcePath, targetPath, mode,
                        existing, doIndex);

                newTargetPath = targetPath + PathHelper.PATH_SEP
                        + PathHelper.clean(path.getFileName().toString());

            } else {

                newTargetPath = targetPath;
            }

            Files.walkFileTree(path, new FileVisitor<Path>() {

                @Override
                public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attrs)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs)
                        throws IOException {
                    return createFileOrFolder(ctx, app, path, file, attrs, sourcePath, newTargetPath, mode,
                            existing, doIndex);
                }

                @Override
                public FileVisitResult visitFileFailed(final Path file, final IOException exc)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
                        throws IOException {
                    return FileVisitResult.CONTINUE;
                }
            });

        } catch (final IOException ex) {
            logger.debug("Mode: " + modeString + ", path: " + sourcePath, ex);
        }
    });

    msg = "Finished direct file import from source directory " + sourcePath + ". Imported " + folderCount
            + " folders and " + fileCount + " files.";
    logger.info(msg);

    publishProgressMessage(msg);
}