Example usage for java.nio.file Path toFile

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

Introduction

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

Prototype

default File toFile() 

Source Link

Document

Returns a File object representing this path.

Usage

From source file:com.excelsiorjet.api.util.Utils.java

public static void cleanDirectory(File f) throws IOException {
    Files.walkFileTree(f.toPath(), new FileVisitor<Path>() {
        @Override/*from w  ww  .  j  av  a  2s  .  c  o m*/
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
            return FileVisitResult.CONTINUE;
        }

        private void deleteFile(File f) throws IOException {
            if (!f.delete()) {
                if (f.exists()) {
                    throw new IOException(Txt.s("JetApi.UnableToDelete.Error", f.getAbsolutePath()));
                }
            }
        }

        @Override
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
            deleteFile(file.toFile());
            return FileVisitResult.CONTINUE;
        }

        @Override
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
            if (file.toFile().exists()) {
                throw new IOException(Txt.s("JetApi.UnableToDelete.Error", f.getAbsolutePath()));
            }
            return FileVisitResult.CONTINUE;
        }

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

From source file:com.liferay.sync.engine.util.SyncClientUpdater.java

public static void update(SyncVersion syncVersion) throws Exception {
    if (syncVersion == null) {
        syncVersion = getLatestSyncVersion();
    }//from ww  w  .  j  a  v  a  2 s  .com

    if (syncVersion == null) {
        return;
    }

    HttpResponse httpResponse = execute(syncVersion.getUrl());

    if (httpResponse == null) {
        return;
    }

    HttpEntity httpEntity = httpResponse.getEntity();
    Path filePath = getFilePath(httpResponse);

    Files.copy(httpEntity.getContent(), filePath, StandardCopyOption.REPLACE_EXISTING);

    Desktop desktop = Desktop.getDesktop();

    desktop.open(filePath.toFile());
}

From source file:com.eternitywall.ots.OtsCli.java

private static HashMap<String, String> readSignature(String file) throws Exception {
    Path path = Paths.get(file);

    if (!path.toFile().exists()) {
        throw new Exception();
    }/*w  w w .java2s . c  om*/

    Properties properties = new Properties();
    properties.load(new FileInputStream(file));
    HashMap<String, String> privateUrls = new HashMap<>();

    for (String key : properties.stringPropertyNames()) {
        String value = properties.getProperty(key);
        privateUrls.put(key, value);
    }

    return privateUrls;
}

From source file:ch.elexis.importer.div.Helpers.java

static Path copyRscToTempDirectory() {
    Path path = null;
    try {/*  w w w.  j a v  a 2s.c om*/
        path = Files.createTempDirectory("HL7_Test");
        File src = new File(PlatformHelper.getBasePath("ch.elexis.core.ui.importer.div.tests"), "rsc");
        System.out.println("src: " + src.toString());
        FileUtils.copyDirectory(src, path.toFile());
    } catch (IOException e) {
        System.out.println(e.getMessage());
        e.printStackTrace();
    }
    return path;
}

From source file:com.eternitywall.ots.OtsCli.java

private static void stamp(Hash hash, List<String> calendarsUrl, Integer m, String signatureFile) {
    HashMap<String, String> privateUrls = new HashMap<>();

    if (signatureFile != null && signatureFile != "") {
        try {/*from  w  w  w  . j ava  2s  . com*/
            privateUrls = readSignature(signatureFile);
        } catch (Exception e) {
            log.severe("No valid signature file");
        }
    }

    String argsOts = Utils.bytesToHex(shasum) + ".ots";
    Path path = Paths.get(argsOts);

    if (path.toFile().exists()) {
        System.out.println("File '" + argsOts + "' already exist");
        return;
    }

    try {
        DetachedTimestampFile detached = DetachedTimestampFile.from(hash);
        Timestamp stampResult = OpenTimestamps.stamp(detached, calendarsUrl, m, privateUrls);
        Files.write(path, stampResult.serialize());
        System.out.println("The timestamp proof '" + argsOts + "' has been created!");
    } catch (Exception e) {
        log.severe("Invalid shasum");
    }
}

From source file:com.sonar.it.scanner.msbuild.TestUtils.java

public static ScannerForMSBuild newScanner(Path projectDir) {
    String scannerVersion = getScannerVersion();

    if (scannerVersion != null) {
        LOG.info("Using Scanner for MSBuild " + scannerVersion);
        return ScannerForMSBuild.create(projectDir.toFile()).setScannerVersion(scannerVersion);
    } else {/*from w w  w.  j  ava 2 s .c  o  m*/
        // run locally
        LOG.info("Using Scanner for MSBuild from the local build");
        Path scannerZip = Paths
                .get("../DeploymentArtifacts/BuildAgentPayload/Release/SonarQube.Scanner.MSBuild.zip");
        return ScannerForMSBuild.create(projectDir.toFile())
                .setScannerLocation(FileLocation.of(scannerZip.toFile()));
    }
}

From source file:com.sonar.it.scanner.msbuild.TestUtils.java

public static Path projectDir(TemporaryFolder temp, String projectName) throws IOException {
    Path projectDir = Paths.get("projects").resolve(projectName);
    FileUtils.deleteDirectory(new File(temp.getRoot(), projectName));
    Path tmpProjectDir = temp.newFolder(projectName).toPath();
    FileUtils.copyDirectory(projectDir.toFile(), tmpProjectDir.toFile());
    return tmpProjectDir;
}

From source file:org.mortbay.jetty.load.generator.starter.AbstractLoadGeneratorStarter.java

protected static Resource evaluateJson(Path profilePath) throws Exception {
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    return objectMapper.readValue(profilePath.toFile(), Resource.class);

}

From source file:Main.java

/**
 * Loads the object from the file./* ww  w  . ja  v  a2 s . c  om*/
 *
 * @param <T> the object type.
 * @param path the XML file.
 * @param clazz the class of the object.
 * @return the corresponding object.
 */
public static <T> T loadObject(Path path, Class<T> clazz) {
    T result;
    if (path == null || clazz == null) {
        throw new RuntimeException("The path to file or class is null!");
    }

    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        result = (T) jaxbUnmarshaller.unmarshal(path.toFile());
    } catch (Exception ex) {
        throw new RuntimeException("Error loading the xml from path " + path.toString(), ex);
    }
    return result;
}

From source file:com.github.checkstyle.NotesBuilder.java

/**
 * Returns a list of commits between two references.
 * @param repoPath path to local git repository.
 * @param startRef start reference./*from w w  w. j  a v a2s .c  o m*/
 * @param endRef end reference.
 * @return a list of commits.
 * @throws IOException if I/O error occurs.
 * @throws GitAPIException if an error occurs when accessing Git API.
 */
private static Set<RevCommit> getCommitsBetweenReferences(String repoPath, String startRef, String endRef)
        throws IOException, GitAPIException {

    final FileRepositoryBuilder builder = new FileRepositoryBuilder();
    final Path path = Paths.get(repoPath);
    final Repository repo = builder.findGitDir(path.toFile()).readEnvironment().build();

    final ObjectId startCommit = getActualRefObjectId(repo, startRef);
    final ObjectId endCommit = getActualRefObjectId(repo, endRef);
    final Iterable<RevCommit> commits = new Git(repo).log().addRange(startCommit, endCommit).call();

    return Sets.newLinkedHashSet(commits);
}