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.github.tteofili.p2h.DataSplitTest.java

@Ignore
@Test//from  w w  w .  j  av a 2s  . c o m
public void testDataSplit() throws Exception {
    String regex = "\\n\\d(\\.\\d)*\\.?\\u0020[\\w|\\-|\\|\\:]+(\\u0020[\\w|\\-|\\:|\\]+){0,10}\\n";
    String prefix = "/path/to/h2v/";
    Pattern pattern = Pattern.compile(regex);
    Path path = Paths.get(getClass().getResource("/papers/raw/").getFile());
    File file = path.toFile();
    if (file.exists() && file.list() != null) {
        for (File doc : file.listFiles()) {
            String s = IOUtils.toString(new FileInputStream(doc));
            String docName = doc.getName();
            File fileDir = new File(prefix + docName);
            assert fileDir.mkdir();
            Matcher matcher = pattern.matcher(s);
            int start = 0;
            String sectionName = "abstract";
            while (matcher.find(start)) {
                String string = matcher.group(0);
                if (isValid(string)) {

                    String content;

                    if (start == 0) {
                        // abstract
                        content = s.substring(0, matcher.start());
                    } else {
                        content = s.substring(start, matcher.start());
                    }

                    File f = new File(prefix + docName + "/" + docName + "_" + sectionName);
                    assert f.createNewFile() : "could not create file" + f.getAbsolutePath();
                    FileOutputStream outputStream = new FileOutputStream(f);
                    IOUtils.write(content, outputStream);

                    start = matcher.end();
                    sectionName = string.replaceAll("\n", "").trim();
                } else {
                    start = matcher.end();
                }
            }
            // remaining
            File f = new File(prefix + docName + "/" + docName + "_" + sectionName);
            assert f.createNewFile();
            FileOutputStream outputStream = new FileOutputStream(f);

            IOUtils.write(s.substring(start), outputStream);
        }
    }
}

From source file:com.sqs.tq.fdc.TemplateReporter.java

public void template(Path dir, String name) throws IOException {
    Configuration cfg = new Configuration(Configuration.VERSION_2_3_23);
    cfg.setDirectoryForTemplateLoading(dir.toFile());
    template = cfg.getTemplate(name);//from w w w . jav  a2 s  .co  m
}

From source file:fr.jetoile.hadoopunit.HadoopBootstrapRemoteStarter.java

private void editHadoopUnitConfFile() {
    Path hadoopPropertiesPath = Paths.get(hadoopUnitPath, "conf", "hadoop.properties");
    Path hadoopPropertiesBackupPath = Paths.get(hadoopUnitPath, "conf", "hadoop.properties.old");
    if (hadoopPropertiesBackupPath.toFile().exists() && hadoopPropertiesBackupPath.toFile().canWrite()) {
        hadoopPropertiesBackupPath.toFile().delete();
    }/*from  w w  w . j  ava2 s.c om*/
    hadoopPropertiesPath.toFile().renameTo(hadoopPropertiesBackupPath.toFile());

    PropertiesConfiguration configuration = new PropertiesConfiguration();

    values.forEach(v -> configuration.addProperty(v.toLowerCase(), "true"));
    try {
        configuration.save(new FileWriter(hadoopPropertiesPath.toFile()));
    } catch (ConfigurationException | IOException e) {
        getLog().error("unable to find or modifying hadoop.properties. Check user rights", e);

    }
}

From source file:de.monticore.io.FileReaderWriter.java

/**
 * Reads the String content from a file using the specified encoding.
 * /*from   www .  ja v  a  2  s  . co m*/
 * @param sourcePath The absolute location (fully specifies the filename) of
 * the file to be read
 * @return The String content of the file
 * @see #setCharset(Charset)
 */
public String readFromFile(Path sourcePath) {
    String content = null;
    try {
        content = FileUtils.readFileToString(sourcePath.toFile(), this.charset);
    } catch (IOException e) {
        Log.error("0xA1024 IOException occured.", e);
        Log.debug("IOException while trying to read the content of " + sourcePath + ".", e,
                this.getClass().getName());
    }
    Log.errorIfNull(content);
    return content;
}

From source file:com.arvato.thoroughly.util.security.impl.DefaultEncryptionKeyRetrievalStrategy.java

private SecretKeySpec loadKeyFromFile() throws Exception {
    try {/* ww  w.j  a v a  2 s .c o  m*/
        // LOGGER.info("Looking for encryption key file in path: {}",
        // this.keyFilePath);
        final Path keyFile = Paths.get(this.keyFilePath, new String[0]);
        if (keyFile.toFile().canRead()) {
            return getKeySpecFromPath(keyFile);
        }

        return tryToLoadKeyFromClasspath();
    } catch (final IOException e) {
        LOGGER.error("Error trying to load encryption key from file", e);
    }
    return null;
}

From source file:fr.jetoile.hadoopunit.HadoopBootstrapRemoteUtils.java

public void tailLogFileUntilFind(Path hadoopLogFilePath, String find, Log log) {
    try {// w w w  .  jav a 2s .co  m
        BufferedReader reader = new BufferedReader(new FileReader(hadoopLogFilePath.toFile()));
        String line;
        boolean keepReading = true;
        while (keepReading) {
            line = reader.readLine();
            if (line == null) {
                //wait until there is more of the file for us to read
                Thread.sleep(1000);
            } else {
                keepReading = !StringUtils.containsIgnoreCase(line, find);
                //do something interesting with the line
            }
        }
    } catch (IOException | InterruptedException e) {
        log.error("unable to read wrapper.log file", e);
    }
}

From source file:com.netflix.spinnaker.halyard.core.registry.v1.LocalDiskProfileReader.java

public InputStream readArchiveProfileFrom(Path profilePath) throws IOException {

    ByteArrayOutputStream os = new ByteArrayOutputStream();
    TarArchiveOutputStream tarArchive = new TarArchiveOutputStream(os);

    ArrayList<Path> filePathsToAdd = java.nio.file.Files
            .walk(profilePath, Integer.MAX_VALUE, FileVisitOption.FOLLOW_LINKS)
            .filter(path -> path.toFile().isFile()).collect(Collectors.toCollection(ArrayList::new));

    for (Path path : filePathsToAdd) {
        TarArchiveEntry tarEntry = new TarArchiveEntry(path.toFile(), profilePath.relativize(path).toString());
        tarArchive.putArchiveEntry(tarEntry);
        IOUtils.copy(Files.newInputStream(path), tarArchive);
        tarArchive.closeArchiveEntry();//from  w  w  w. ja  v a 2 s.c o m
    }

    tarArchive.finish();
    tarArchive.close();

    return new ByteArrayInputStream(os.toByteArray());
}

From source file:de.dentrassi.pm.npm.aspect.NpmExtractor.java

private void perform(final Path file, final Map<String, String> metadata) throws IOException {
    try (final GZIPInputStream gis = new GZIPInputStream(new FileInputStream(file.toFile()));
            final TarArchiveInputStream tis = new TarArchiveInputStream(gis)) {
        TarArchiveEntry entry;/*from ww  w  . j a v a2  s  .c  o m*/
        while ((entry = tis.getNextTarEntry()) != null) {
            if (entry.getName().equals("package/package.json")) {
                final byte[] data = new byte[(int) entry.getSize()];
                ByteStreams.read(tis, data, 0, data.length);

                final String str = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(data)).toString();

                try {
                    // test parse
                    new JsonParser().parse(str);
                    // store
                    metadata.put("package.json", str);
                } catch (final JsonParseException e) {
                    // ignore
                }

                break; // stop parsing the archive
            }
        }

    }
}

From source file:com.ibm.vicos.client.ClientCommand.java

@CliCommand(value = "putblob", help = "Uploads a blob from source path")
public String putBlob(
        @CliOption(key = { "container" }, mandatory = true, help = "container name") final String container,
        @CliOption(key = { "src" }, mandatory = true, help = "source file") final String src) {
    Path fullPath = FileSystems.getDefault().getPath(src);
    final File file = fullPath.toFile();
    try {//from w w w .jav  a2s.c  o  m
        InputStream data = new FileInputStream(file);
        storage.createObject(container, file.getName(), data, file.length());
    } catch (FileNotFoundException e) {
        return src + " does not exists";
    } catch (Exception e) {
        e.printStackTrace();
        return "Error while creating Object";
    }
    return "Successfully uploaded " + container + "/" + file.getName();
}

From source file:tpt.dbweb.cat.evaluation.ComparisonResult.java

public void write(Path file) throws JsonGenerationException, JsonMappingException, IOException {
    mapper.enable(SerializationFeature.INDENT_OUTPUT);
    mapper.writeValue(file.toFile(), this);
}