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:ch.bender.evacuate.Testconstants.java

/**
 * Creates a new file with given name in given parent directory.
 * <p>/* w ww  . j a v a 2 s .co  m*/
 * The path of the new file is written into the file.
 * <p>
 * 
 * @param aParent
 *        must be an existing directory       
 * @param aNewFileName
 *        the name of the new file
 * @return the path object of the new file
 * @throws IOException
 */
public static Path createNewFile(Path aParent, String aNewFileName) throws IOException {
    if (!Files.isDirectory(aParent)) {
        throw new IllegalArgumentException("Given parent is not a directory or does not exist");
    }

    Path file1 = Paths.get(aParent.toString(), aNewFileName);
    Files.createFile(file1);
    FileUtils.writeStringToFile(file1.toFile(), file1.toString() + "\n");
    return file1;
}

From source file:com.pixlabs.web.utils.Mp3Finder.java

/**
 * @param path Path of the directory that should be looked into.
 * @return a linkedlist containing all the Mp3 files found in the directory.
 * @throws NotDirectoryException The given path was not a directory.
 *//* ww  w . j  a va 2 s  .  c  o m*/

public static LinkedList<Mp3FileAdvanced> mp3InDirectory(Path path) throws NotDirectoryException {
    if (!Files.isDirectory(path))
        throw new NotDirectoryException("The chosen path does not represent a directory");
    LinkedList<Mp3FileAdvanced> list = new LinkedList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(path, "*.mp3")) {
        for (Path entry : stream) {
            //       if(!entry.startsWith("."))
            list.add(new Mp3FileAdvanced(entry.toFile()));
        }

    } catch (IOException | UnsupportedTagException | InvalidDataException e) {
        e.printStackTrace();
    }
    return list;
}

From source file:com.flipkart.poseidon.api.APIManager.java

public static void scanAndAdd(Path dir, List<String> validConfigs) {
    try (DirectoryStream<Path> files = Files.newDirectoryStream(dir)) {
        for (Path entry : files) {
            File file = entry.toFile();
            if (file.isDirectory()) {
                scanAndAdd(entry, validConfigs);
                continue;
            }/*www .jav  a2 s  .c o m*/
            if ("json".equals(FilenameUtils.getExtension(file.getName()))) {
                try {
                    String config = FileUtils.readFileToString(file);
                    if (validateConfig(config)) {
                        validConfigs.add(config);
                    }
                } catch (IOException e) {
                    logger.error(
                            "Unable to read one of the local config. Filename = [[" + file.getName() + "]]");
                }
            }
        }
    } catch (IOException e) {
        logger.error("Local override directory not found.");
    }

}

From source file:cd.go.contrib.elasticagents.docker.DockerClientFactory.java

private static void setupCerts(PluginSettings pluginSettings, DefaultDockerClient.Builder builder)
        throws IOException, DockerCertificateException {
    if (isBlank(pluginSettings.getDockerCACert()) || isBlank(pluginSettings.getDockerClientCert())
            || isBlank(pluginSettings.getDockerClientKey())) {
        LOG.warn("Missing docker certificates, will attempt to connect without certificates");
        return;/*from  w  w  w  .  jav  a  2s.c o m*/
    }

    Path certificateDir = Files.createTempDirectory(UUID.randomUUID().toString());
    File tempDirectory = certificateDir.toFile();

    try {
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CA_CERT_NAME),
                pluginSettings.getDockerCACert(), StandardCharsets.UTF_8);
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CLIENT_CERT_NAME),
                pluginSettings.getDockerClientCert(), StandardCharsets.UTF_8);
        FileUtils.writeStringToFile(new File(tempDirectory, DockerCertificates.DEFAULT_CLIENT_KEY_NAME),
                pluginSettings.getDockerClientKey(), StandardCharsets.UTF_8);
        builder.dockerCertificates(new DockerCertificates(certificateDir));
    } finally {
        FileUtils.deleteDirectory(tempDirectory);
    }
}

From source file:com.thesmartguild.firmloader.lib.tftp.TFTPServerFactory.java

public static ServerInfo instantiate() {
    try {//from   ww w  .  ja  va 2 s  .c  o  m
        Path tmpFilePath = TFTPServerFactory.createTempDirectory();
        TFTPServer serv = new TFTPServer(tmpFilePath.toFile(), tmpFilePath.toFile(), ServerMode.GET_ONLY);
        return new TFTPServerFactory().new ServerInfo(serv, tmpFilePath);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:it.tidalwave.accounting.importer.ibiz.impl.IBizUtils.java

/*******************************************************************************************************************
 *
 * @param   path         the path of the configuration file
 * @return               the configuration object
 * @throws  IOException  in case of error
 * //from  w w w  . j av  a  2s . co m
 ******************************************************************************************************************/
@Nonnull
public static ConfigurationDecorator loadConfiguration(final @Nonnull Path path) throws IOException {
    try {
        return new ConfigurationDecorator(new XMLPropertyListConfiguration(path.toFile()));
    } catch (ConfigurationException e) {
        throw new IOException(e);
    }
}

From source file:com.fizzed.blaze.jdk.BlazeJdkEngineTest.java

@BeforeClass
static public void clearCache() throws IOException {
    Context context = new ContextImpl(null, Paths.get(System.getProperty("user.home")), null, null);
    Path classesDir = ConfigHelper.userBlazeEngineDir(context, "java");
    FileUtils.deleteDirectory(classesDir.toFile());
}

From source file:net.certiv.antlr.project.util.Utils.java

/**
 * Clears (deletes) all files from the given directory.
 * /* w  w  w  .  ja  v a2 s .  com*/
 * @param dir
 *            the directory to clear
 * @return true if all files were successfully deleted
 * @throws IOException
 */
public static boolean deleteAllFiles(File dir) throws IOException {
    if (dir == null)
        throw new IllegalArgumentException("Directory cannot be null");
    if (!dir.exists() || !dir.isDirectory())
        throw new IOException("Directory must exist");

    DirectoryStream<Path> ds = Files.newDirectoryStream(dir.toPath());
    int del = 0;
    int tot = 0;
    for (Path p : ds) {
        File f = p.toFile();
        String name = f.getName();
        if (f.isFile()) {
            tot++;
            boolean ok = f.delete();
            if (ok) {
                del++;
            } else {
                Log.warn(Utils.class, "Failed to delete: " + name);
            }
        }
    }
    Log.info(Utils.class, "Deleted " + del + " of " + tot + " files");
    return del == tot;
}

From source file:com.arpnetworking.metrics.mad.performance.CollectdPipelinePT.java

@BeforeClass
public static void setUp() throws IOException, URISyntaxException {
    // Extract the sample file
    final Path gzipPath = Paths.get(Resources.getResource("collectd-sample1.log.gz").toURI());
    final FileInputStream fileInputStream = new FileInputStream(gzipPath.toFile());
    final GZIPInputStream gzipInputStream = new GZIPInputStream(fileInputStream);
    final Path path = Paths.get("target/tmp/perf/collectd-sample1.log");
    final FileOutputStream outputStream = new FileOutputStream(path.toFile());

    IOUtils.copy(gzipInputStream, outputStream);

    JSON_BENCHMARK_CONSUMER.prepareClass();
}

From source file:com.c4om.autoconf.ulysses.configanalyzer.packager.CompressedFileConfigurationPackager.java

/**
 * It takes an absolute {@link File} and returns a {@link File} relative to
 * another given base {@link File}.//w  w w  . j a va  2 s  .  c o  m
 * 
 * @param absoluteFile the absolute File
 * @param baseFile the base File
 * @return a File relative to baseFile, pointing to the same file than
 *         absoluteFile
 */
protected static File relativizeFile(File absoluteFile, File baseFile) {
    Path absolutePath = absoluteFile.toPath();
    Path basePath = baseFile.toPath();
    Path relativePath = basePath.relativize(absolutePath);
    return relativePath.toFile();
}