Example usage for org.apache.commons.io FileUtils readFileToString

List of usage examples for org.apache.commons.io FileUtils readFileToString

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils readFileToString.

Prototype

public static String readFileToString(File file) throws IOException 

Source Link

Document

Reads the contents of a file into a String using the default encoding for the VM.

Usage

From source file:jenkins.model.lazy.FakeMap.java

@Override
protected Build retrieve(File dir) throws IOException {
    String n = FileUtils.readFileToString(new File(dir, "n")).trim();
    //new Exception("loading #" + n).printStackTrace();
    return new Build(Integer.parseInt(n));
}

From source file:com.hurence.logisland.documentation.DocGeneratorTest.java

@Test
@Ignore//from  w ww .  j av  a2s.c  o m
public void testProcessor() throws IOException, ClassNotFoundException {
    TemporaryFolder temporaryFolder = new TemporaryFolder();
    temporaryFolder.create();

    /*ExtensionManager.discoverExtensions(NarClassLoaders.getInstance().getExtensionClassLoaders());*/

    DocGenerator.generate(new File("docs"), "rst");

    File processorDirectory = new File(temporaryFolder.getRoot(),
            "com.hurence.logisland.processors.WriteResourceToStream");
    File indexHtml = new File(processorDirectory, "index.html");
    Assert.assertTrue(indexHtml + " should have been generated", indexHtml.exists());
    String generatedHtml = FileUtils.readFileToString(indexHtml);
    Assert.assertNotNull(generatedHtml);
    Assert.assertTrue(generatedHtml.contains(
            "This example processor loads a resource from the nar and writes it to the Record content"));
    Assert.assertTrue(generatedHtml.contains("files that were successfully processed"));
    Assert.assertTrue(generatedHtml.contains("files that were not successfully processed"));
    Assert.assertTrue(generatedHtml.contains("resources"));
}

From source file:com.github.terma.jenkins.githubprcoveragestatus.CoberturaParser.java

@Override
public float get(String coberturaFilePath) {
    try {//from w ww. ja  va 2 s .co m
        String content = FileUtils.readFileToString(new File(coberturaFilePath));
        float lineRate = Float.parseFloat(findFirst(content, "line-rate=\"([0-9.]+)\""));
        float branchRate = Float.parseFloat(findFirst(content, "branch-rate=\"([0-9.]+)\""));
        return lineRate / 2 + branchRate / 2;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.cloud.utils.ProcessUtil.java

public static void pidCheck(String pidDir, String run) throws ConfigurationException {

    String dir = pidDir == null ? "/var/run" : pidDir;

    try {/*from www  .j a v a  2 s . c  om*/
        final File propsFile = PropertiesUtil.findConfigFile("environment.properties");
        if (propsFile == null) {
            s_logger.debug("environment.properties could not be opened");
        } else {
            final Properties props = PropertiesUtil.loadFromFile(propsFile);
            dir = props.getProperty("paths.pid");
            if (dir == null) {
                dir = pidDir == null ? "/var/run" : pidDir;
            }
        }
    } catch (IOException e) {
        s_logger.debug("environment.properties could not be opened");
    }

    final File pidFile = new File(dir + File.separator + run);
    try {
        if (!pidFile.createNewFile()) {
            if (!pidFile.exists()) {
                throw new ConfigurationException("Unable to write to " + pidFile.getAbsolutePath()
                        + ".  Are you sure you're running as root?");
            }

            final String pidLine = FileUtils.readFileToString(pidFile).trim();
            if (pidLine.isEmpty()) {
                throw new ConfigurationException(
                        "Java process is being started twice.  If this is not true, remove "
                                + pidFile.getAbsolutePath());
            }
            try {
                final long pid = Long.parseLong(pidLine);
                final Script script = new Script("bash", 120000, s_logger);
                script.add("-c", "ps -p " + pid);
                final String result = script.execute();
                if (result == null) {
                    throw new ConfigurationException(
                            "Java process is being started twice.  If this is not true, remove "
                                    + pidFile.getAbsolutePath());
                }
                if (!pidFile.delete()) {
                    throw new ConfigurationException(
                            "Java process is being started twice.  If this is not true, remove "
                                    + pidFile.getAbsolutePath());
                }
                if (!pidFile.createNewFile()) {
                    throw new ConfigurationException(
                            "Java process is being started twice.  If this is not true, remove "
                                    + pidFile.getAbsolutePath());
                }
            } catch (final NumberFormatException e) {
                throw new ConfigurationException(
                        "Java process is being started twice.  If this is not true, remove "
                                + pidFile.getAbsolutePath());
            }
        }
        pidFile.deleteOnExit();

        final Script script = new Script("bash", 120000, s_logger);
        script.add("-c", "echo $PPID");
        final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
        script.execute(parser);

        final String pid = parser.getLine();

        FileUtils.writeStringToFile(pidFile, pid + "\n");
    } catch (final IOException e) {
        throw new CloudRuntimeException(
                "Unable to create the " + pidFile.getAbsolutePath() + ".  Are you running as root?", e);
    }
}

From source file:hu.bme.mit.trainbenchmark.benchmark.sql.queries.SqlQuery.java

public SqlQuery(final TSqlDriver driver, final String workspaceDir, final RailwayQuery query)
        throws IOException, SQLException {
    super(query, driver);

    final String queryPath = workspaceDir + driver.getResourceDirectory() + "queries/" + query + ".sql";
    queryDefinition = FileUtils.readFileToString(new File(queryPath));
}

From source file:fr.dutra.confluence2wordpress.core.metadata.MetadataManagerTest.java

@Before
public void prepare() throws IOException {
    json = FileUtils.readFileToString(new File("src/test/resources/metadata.json"));
}

From source file:com.sqatntu.stylechecker.configuration.JsonConfigurationLoader.java

@Override
public Configuration loadFileConfiguration(String filePath) throws IOException {
    File configurationFile = new File(filePath);
    String configurationJson = FileUtils.readFileToString(configurationFile);

    return loadJsonConfiguration(configurationJson);
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.createdebate.CorpusPreparator.java

/**
 * Extracts all debates from raw HTML files in inFolder and stores them into outFolder
 * as serialized {@link Debate} objects (see {@link DebateSerializer}.
 *
 * @param inFolder     in folder//from   w w  w . j a  va  2 s. co  m
 * @param outFolder    out folder (must exist)
 * @param debateParser debate parser implementation
 * @throws IOException exception
 */
public static void extractAllDebates(File inFolder, File outFolder, DebateParser debateParser)
        throws IOException {
    File[] files = inFolder.listFiles();
    if (files == null) {
        throw new IOException("No such dir: " + inFolder);
    }

    for (File f : files) {
        InputStream inputStream = new FileInputStream(f);
        try {
            Debate debate = debateParser.parseDebate(inputStream);

            // we ignore empty debates (without arguments)
            if (debate != null && !debate.getArgumentList().isEmpty()) {
                // serialize to xml
                String xml = DebateSerializer.serializeToXML(debate);

                // same name with .xml
                File outputFile = new File(outFolder, f.getName() + ".xml");

                FileUtils.writeStringToFile(outputFile, xml, "utf-8");
                System.out.println("Saved to " + outputFile.getAbsolutePath());

                // ensure we can read it again
                DebateSerializer.deserializeFromXML(FileUtils.readFileToString(outputFile));
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            IOUtils.closeQuietly(inputStream);
        }
    }

}

From source file:de.tudarmstadt.ukp.dkpro.core.flextag.InstallAsMavenArtifact.java

private static String prepareBuildScript(String modelLocation, String groupId, String artifactId,
        String version, String tool, String language, String variant, String targetName,
        String workingDirectory, String modelencoding, String postagset) throws IOException {

    String buildFile = FileUtils
            .readFileToString(new File("src/main/resources/installModelAsMavenArtifact/build.xml"));
    buildFile = buildFile.replaceAll("#TARGETNAME#", targetName);
    buildFile = buildFile.replaceAll("#MODELLOCATION#", modelLocation);
    buildFile = buildFile.replaceAll("#GROUPID#", groupId);
    buildFile = buildFile.replaceAll("#ARTIFACTID#", artifactId);
    buildFile = buildFile.replaceAll("#DATE#", version);
    buildFile = buildFile.replaceAll("#VERSION#", "1");
    buildFile = buildFile.replaceAll("#TOOL#", tool);
    buildFile = buildFile.replaceAll("#LANGUAGE#", language);
    buildFile = buildFile.replaceAll("#VARIANT#", variant);
    buildFile = buildFile.replaceAll("#ENCODING#", modelencoding);
    buildFile = buildFile.replaceAll("#POSTAGSET#", postagset);
    return buildFile;
}

From source file:com.htmlhifive.pitalium.common.util.JSONUtilsTest.java

@Test
public void testWriteValue() throws Exception {
    file = File.createTempFile("tmp", null);
    JSONUtils.writeValue(file, data);/*from   w  ww .  j  a  va 2 s.c o  m*/

    String result = FileUtils.readFileToString(file);
    assertThat(result, is("{\"a\":\"b\"}"));
}