Example usage for java.nio.file Path getParent

List of usage examples for java.nio.file Path getParent

Introduction

In this page you can find the example usage for java.nio.file Path getParent.

Prototype

Path getParent();

Source Link

Document

Returns the parent path, or null if this path does not have a parent.

Usage

From source file:ws.doerr.cssinliner.server.PathSerializer.java

@Override
public void serialize(Path value, JsonGenerator gen, SerializerProvider serializers)
        throws IOException, JsonProcessingException {
    gen.writeStartObject();/*w w  w.j ava 2 s.com*/
    gen.writeStringField("name", value.getFileName().toString());
    gen.writeStringField("folder", value.getParent().toString());
    gen.writeStringField("path", value.toString());
    gen.writeNumberField("modified", value.toFile().lastModified());
    gen.writeEndObject();
}

From source file:com.streamsets.pipeline.lib.io.FileContext.java

public FileContext(MultiFileInfo multiFileInfo, Charset charset, int maxLineLength,
        PostProcessingOptions postProcessing, String archiveDir, FileEventPublisher eventPublisher)
        throws IOException {
    open = true;//from  w  ww  . ja va2s. c  o m
    this.multiFileInfo = multiFileInfo;
    this.charset = charset;
    this.maxLineLength = maxLineLength;
    this.postProcessing = postProcessing;
    this.archiveDir = archiveDir;
    this.eventPublisher = eventPublisher;
    Path fullPath = Paths.get(multiFileInfo.getFileFullPath());
    dir = fullPath.getParent();
    Path name = fullPath.getFileName();
    rollMode = multiFileInfo.getFileRollMode().createRollMode(name.toString(), multiFileInfo.getPattern());
    scanner = new LiveDirectoryScanner(dir.toString(), multiFileInfo.getFirstFile(), getRollMode());
}

From source file:edu.cornell.mannlib.vitro.webapp.rdfservice.impl.jena.tdb.RDFServiceTDB.java

public RDFServiceTDB(String directoryPath) throws IOException {
    Path tdbDir = Paths.get(directoryPath);

    if (!Files.exists(tdbDir)) {
        Path parentDir = tdbDir.getParent();
        if (!Files.exists(parentDir)) {
            throw new IllegalArgumentException(
                    "Cannot create TDB directory '" + tdbDir + "': parent directory does not exist.");
        }/*from  w w w.jav  a  2  s . com*/
        Files.createDirectory(tdbDir);
    }

    this.dataset = TDBFactory.createDataset(directoryPath);
}

From source file:au.edu.uq.cmm.paul.queue.AbstractQueueFileManager.java

@Override
public FileStatus getFileStatus(File file) {
    Path path = file.toPath();
    File parent = path.getParent().toFile();
    if (Files.exists(path)) {
        if (parent.equals(captureDirectory)) {
            if (Files.isSymbolicLink(path)) {
                return FileStatus.CAPTURED_SYMLINK;
            } else {
                return FileStatus.CAPTURED_FILE;
            }/*from   w w w  . ja v  a 2s . c o  m*/
        } else if (parent.equals(archiveDirectory)) {
            if (Files.isSymbolicLink(path)) {
                return FileStatus.ARCHIVED_SYMLINK;
            } else {
                return FileStatus.ARCHIVED_FILE;
            }
        } else {
            return FileStatus.NOT_OURS;
        }
    } else {
        if (Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
            if (parent.equals(captureDirectory)) {
                return FileStatus.BROKEN_CAPTURED_SYMLINK;
            } else if (parent.equals(archiveDirectory)) {
                return FileStatus.BROKEN_ARCHIVED_SYMLINK;
            } else {
                return FileStatus.NOT_OURS;
            }
        } else {
            return FileStatus.NON_EXISTENT;
        }
    }
}

From source file:org.ng200.openolympus.controller.task.TaskFilesystemManipulatingController.java

@PreAuthorize(SecurityExpressionConstants.IS_ADMIN)
private void extractZipFile(final InputStream zipFile, final Path destination) throws Exception {
    try (ArchiveInputStream input = new ArchiveStreamFactory()
            .createArchiveInputStream(new BufferedInputStream(zipFile))) {
        ArchiveEntry entry;/*from www. j  a  va2  s.  c om*/
        while ((entry = input.getNextEntry()) != null) {
            final Path dest = destination.resolve(entry.getName());
            if (entry.isDirectory()) {
                FileAccess.createDirectories(dest);
            } else {
                FileAccess.createDirectories(dest.getParent());
                FileAccess.createFile(dest);
                Files.copy(input, dest, StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }
}

From source file:nl.salp.warcraft4j.casc.cdn.online.CachingOnlineDataReaderProvider.java

/**
 * Cache a file, overwriting the previous version if existing.
 *
 * @param url The URL of the file to cache.
 *
 * @return The path of the cached file.//from  ww w.  ja  v a  2  s  . c  o  m
 *
 * @throws IOException When reading the file or writing the cached version of the file fails.
 */
private Path cache(String url) throws IOException {
    Path file = toCacheFile(url);
    LOGGER.trace("Caching CDN file {} to {}", url, file);
    if (!Files.exists(file.getParent())) {
        LOGGER.trace("Creating directory structure {} to cache file {}", file.getParent(), file);
        Files.createDirectories(file.getParent());
    }
    try (DataReader fileDataReader = new CachedHttpDataReader(url);
            ByteChannel cachedFileChannel = Files.newByteChannel(file, CREATE, WRITE, TRUNCATE_EXISTING)) {
        long fileSize = 0;
        while (fileDataReader.hasRemaining()) {
            int chunkSize = (int) Math.min(CACHE_CHUNK_SIZE, fileDataReader.remaining());
            ByteBuffer dataBuffer = ByteBuffer
                    .wrap(fileDataReader.readNext(DataTypeFactory.getByteArray(chunkSize)));
            while (dataBuffer.hasRemaining()) {
                fileSize += cachedFileChannel.write(dataBuffer);
            }
        }
        LOGGER.trace("Cached {} byte CDN file {} to {}", fileSize, url, file);
    }
    return file;
}

From source file:com.collaborne.jsonschema.generator.pojo.PojoGeneratorSmokeTest.java

@After
public void tearDown() throws IOException {
    // Dump the contents of the file system
    Path dumpStart = fs.getPath("/");
    Files.walkFileTree(dumpStart, new SimpleFileVisitor<Path>() {
        @Override/*from  w w  w  .j a v  a  2s .c om*/
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            System.out.println("> " + file);
            System.out.println(new String(Files.readAllBytes(file), StandardCharsets.UTF_8));

            // Copy the file if wanted
            if (DUMP_DIRECTORY != null) {
                Path dumpTarget = Paths.get(DUMP_DIRECTORY, name.getMethodName());
                Path target = dumpTarget.resolve(dumpStart.relativize(file).toString());
                Files.createDirectories(target.getParent());
                Files.copy(file, target, StandardCopyOption.REPLACE_EXISTING);
            }
            return FileVisitResult.CONTINUE;
        }
    });
}

From source file:org.jboss.as.test.manualmode.logging.SizeAppenderRestartTestCase.java

private void clearLogs(final Path path) throws IOException {
    final String expectedName = path.getFileName().toString();
    Files.walkFileTree(path.getParent(), new SimpleFileVisitor<Path>() {
        @Override//from   ww w .  j  a v a2 s.c  o m
        public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException {
            final String currentName = file.getFileName().toString();
            if (currentName.startsWith(expectedName)) {
                Files.delete(file);
            }
            return super.visitFile(file, attrs);
        }
    });
}

From source file:com.relicum.ipsum.io.PropertyIO.java

/**
 * Create a new directory including any sub directories that is required.
 *
 * @param file the {@link java.nio.file.Path} of the file or directory
 * @throws RuntimeException if an error occured when trying to create the directory
 */// ww w .j a  v a 2 s .  c o  m
default void createDirectories(Path file) throws RuntimeException {
    Validate.notNull(file);

    Path parent = file.getParent();

    try {
        Files.createDirectories(parent);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

}

From source file:org.datavec.audio.recordreader.BaseAudioRecordReader.java

@Override
public void initialize(InputSplit split) throws IOException, InterruptedException {
    inputSplit = split;//from   www.ja  v a2 s. c  o m
    if (split instanceof BaseInputSplit) {
        URI[] locations = split.locations();
        if (locations != null && locations.length >= 1) {
            if (locations.length > 1) {
                List<File> allFiles = new ArrayList<>();
                for (URI location : locations) {
                    File iter = new File(location);
                    if (iter.isDirectory()) {
                        Iterator<File> allFiles2 = FileUtils.iterateFiles(iter, null, true);
                        while (allFiles2.hasNext())
                            allFiles.add(allFiles2.next());
                    }

                    else
                        allFiles.add(iter);
                }

                iter = allFiles.iterator();
            } else {
                File curr = new File(locations[0]);
                if (curr.isDirectory())
                    iter = FileUtils.iterateFiles(curr, null, true);
                else
                    iter = Collections.singletonList(curr).iterator();
            }
        }
    }

    else if (split instanceof InputStreamInputSplit) {
        record = new ArrayList<>();
        InputStreamInputSplit split2 = (InputStreamInputSplit) split;
        InputStream is = split2.getIs();
        URI[] locations = split2.locations();
        if (appendLabel) {
            Path path = Paths.get(locations[0]);
            String parent = path.getParent().toString();
            record.add(new DoubleWritable(labels.indexOf(parent)));
        }

        is.close();
    }

}