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:org.transitime.gtfs.GtfsUpdatedModule.java

/**
 * Copies the specified file to a directory at the same directory level but
 * with the directory name that is the last modified date of the file (e.g.
 * 03-28-2015)./*  w  ww. java2 s  . com*/
 * 
 * @param fullFileName
 *            The full name of the file to be copied
 */
private static void archive(String fullFileName) {
    // Determine name of directory to archive file into. Use date of
    // lastModified time of file e.g. MM-dd-yyyy.
    File file = new File(fullFileName);
    Date lastModified = new Date(file.lastModified());
    String dirName = Time.dateStr(lastModified);

    // Copy the file to the sibling directory with the name that is the
    // last modified date (e.g. 03-28-2015)
    Path source = Paths.get(fullFileName);
    Path target = source.getParent().getParent().resolve(dirName).resolve(source.getFileName());

    logger.info("Archiving file {} to {}", source.toString(), target.toString());

    try {
        // Create the directory where file is to go
        String fullDirName = target.getParent().toString();
        new File(fullDirName).mkdir();

        // Copy the file to the directory
        Files.copy(source, target, StandardCopyOption.COPY_ATTRIBUTES);
    } catch (IOException e) {
        logger.error("Was not able to archive GTFS file {} to {}", source.toString(), target);
    }
}

From source file:io.github.robwin.diff.DiffAssert.java

private static void writeHtmlReport(Path reportPath, LinkedList<DiffMatchPatch.Diff> diffs) {
    DiffMatchPatch differ = new DiffMatchPatch();
    try {// w w  w  .ja  v  a  2s .c o m
        Files.createDirectories(reportPath.getParent());
        try (BufferedWriter writer = Files.newBufferedWriter(reportPath, Charset.forName("UTF-8"))) {
            writer.write(differ.diff_prettyHtml(diffs));
        }
    } catch (IOException e) {
        throw new RuntimeException(String.format("Failed to write report %s", reportPath.toAbsolutePath()), e);
    }
}

From source file:org.sakuli.datamodel.helper.TestCaseStepHelper.java

public static List<TestCaseStep> readCachedStepDefinitions(Path tcFile) throws IOException {
    List<TestCaseStep> result = new ArrayList<>();
    if (tcFile != null) {
        Path stepsCacheFile = tcFile.getParent().resolve(STEPS_CACHE_FILE);
        if (Files.exists(stepsCacheFile)) {
            try {
                List<String> lines = FileUtils.readLines(stepsCacheFile.toFile(), Charset.forName("UTF-8"));
                DateTime creationDate = new DateTime();
                for (String line : lines) {
                    if (StringUtils.isNotEmpty(line)) {
                        TestCaseStep step = new TestCaseStepBuilder(line).withCreationDate(creationDate)
                                .build();
                        result.add(step);
                        LOGGER.debug("add cached step definition: " + step);
                        //increase creation date to ensure correct sorting
                        creationDate = creationDate.plusMillis(1);
                    }// w w w.  j  av  a 2s. com
                }
            } catch (IOException e) {
                throw new IOException(
                        String.format("Can't read in cached step definitions in file '%s'", stepsCacheFile), e);
            }
        }
    }
    return result;
}

From source file:com.surevine.gateway.scm.git.jgit.TestUtility.java

public static LocalRepoBean createBareClone(final LocalRepoBean nonBareSource) throws Exception {
    final String projectKey = nonBareSource.getProjectKey();
    final String repoSlug = nonBareSource.getSlug() + "_bare";
    final String cloneURL = nonBareSource.getRepoDirectory().toString();
    final Path repoPath = Paths.get(PropertyUtil.getGitDir(), "local_scm", projectKey, repoSlug);
    Files.createDirectories(repoPath.getParent());

    final CloneCommand cloneCommand = new CloneCommand();
    cloneCommand.setBare(true).setCloneAllBranches(true).setURI(cloneURL).call();

    final LocalRepoBean repoBean = new LocalRepoBean();
    repoBean.setProjectKey(projectKey);//from  w ww  .  j  a v a  2s  .  c om
    repoBean.setSlug(repoSlug);
    repoBean.setLocalBare(true);
    return repoBean;
}

From source file:org.reactor.monitoring.util.FileHandler.java

public static void createAndUpdateFile(final boolean replaceFile, final String fileLocation,
        final String output) {
    try {/*from  w  w  w . jav  a 2 s  . co m*/
        Path path = Paths.get(fileLocation);
        Path parentDir = path.getParent();
        if (!Files.exists(parentDir)) {
            Files.createDirectories(parentDir);
        }

        if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
            Files.createFile(path);
        } else if (replaceFile) {
            Files.delete(path);
            Files.createFile(path);
        }

        Files.write(path, output.getBytes(), StandardOpenOption.APPEND);

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:org.ng200.openolympus.FileAccess.java

public static void copyDirectory(final Path from, final Path to, CopyOption... copyOptions) throws IOException {
    try (Stream<Path> files = Files.walk(from)) {
        for (final Path file : files.collect(Collectors.toList())) {
            final Path target = to.resolve(from.relativize(file));
            Files.createDirectories(target.getParent());
            Files.copy(file, target, copyOptions);
        }// www .j a v  a  2  s  .  c  o  m
    }
}

From source file:Main.java

private static Path getPathToBuckConfig(Path startPath) {
    Path curPath = startPath;
    while (curPath != null) {
        Path pathToBuckConfig = curPath.resolve(BUCK_CONFIG_FILE);
        if (pathToBuckConfig.toFile().exists()) {
            return pathToBuckConfig;
        }/*from   w  ww . ja va 2 s.com*/
        curPath = curPath.getParent();
    }
    return null;
}

From source file:org.apache.nifi.toolkit.tls.standalone.TlsToolkitStandaloneCommandLine.java

protected static String calculateDefaultOutputDirectory(Path currentPath) {
    Path currentAbsolutePath = currentPath.toAbsolutePath();
    Path parent = currentAbsolutePath.getParent();
    if (parent == currentAbsolutePath.getRoot()) {
        return parent.toString();
    } else {//w  w w  . j  a  v  a 2s  . c  o m
        Path currentNormalizedPath = currentAbsolutePath.normalize();
        return "../" + currentNormalizedPath.getFileName().toString();
    }
}

From source file:org.reactor.monitoring.util.FileHandler.java

public static Properties loadPropertiesFromFile(final String filePath) throws IOException {
    // reading property file
    Path path = Paths.get(filePath);

    // If it doesn't exist
    Path parentDir = path.getParent();
    if (!Files.exists(parentDir)) {
        log.info("config directory doesn't exist");
        Files.createDirectories(parentDir);
    }/*  w  w  w.ja va  2 s. c  o m*/

    if (!Files.exists(path, LinkOption.NOFOLLOW_LINKS)) {
        log.info("config file doesn't exist");
        Files.createFile(path);
    }

    Properties properties = new Properties();
    byte[] bytes = readAllBytes(path);
    InputStream myInputStream = new ByteArrayInputStream(bytes);
    properties.load(myInputStream);

    return properties;
}

From source file:cloudeventbus.pki.CertificateUtils.java

/**
 * Saves a collection of certificates to the specified file.
 *
 * @param fileName the file to which the certificates will be saved
 * @param certificates the certificate to be saved
 * @throws IOException if an I/O error occurs
 *//* ww  w  . j  av  a 2  s.co  m*/
public static void saveCertificates(String fileName, Collection<Certificate> certificates) throws IOException {
    final Path path = Paths.get(fileName);
    final Path directory = path.getParent();
    if (directory != null && !Files.exists(directory)) {
        Files.createDirectories(directory);
    }
    try (final OutputStream fileOut = Files.newOutputStream(path, StandardOpenOption.CREATE,
            StandardOpenOption.TRUNCATE_EXISTING); final OutputStream out = new Base64OutputStream(fileOut)) {
        CertificateStoreLoader.store(out, certificates);
    }
}