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, String encoding) throws IOException 

Source Link

Document

Reads the contents of a file into a String.

Usage

From source file:cz.incad.kramerius.k5indexer.FieldsConfig.java

public FieldsConfig() throws IOException, JSONException {
    String path = Constants.WORKING_DIR + File.separator + "k5indexer" + File.separator + "fields.json";
    File f = new File(path);
    if (!f.exists() || !f.canRead()) {
        f = FileUtils/*from  w w  w .j  a v  a 2 s. c  om*/
                .toFile(KrameriusDocument.class.getResource("/cz/incad/kramerius/k5indexer/res/fields.json"));
    }
    String json = FileUtils.readFileToString(f, "UTF-8");
    fieldsConfig = new JSONObject(json);
    mappings = fieldsConfig.getJSONObject("mappings");
    streams = fieldsConfig.getJSONObject("datastreams");
}

From source file:com.wavemaker.tools.ant.MergeUserWebXmlTask.java

@Override
public void execute() {
    if (this.workdir == null) {
        throw new IllegalArgumentException("private String workDir; is not set");
    }/*from w ww .  j  a v a  2  s .  c om*/

    File userwebxml = new File(this.workdir, ProjectConstants.USER_WEB_XML);
    File webxml = new File(this.workdir, ProjectConstants.WEB_XML);

    try {
        int indx1, indx2, indx3;
        String content = FileUtils.readFileToString(webxml, ServerConstants.DEFAULT_ENCODING);
        String customcontent = FileUtils.readFileToString(userwebxml, ServerConstants.DEFAULT_ENCODING)
                + "\r\n";

        indx1 = customcontent.indexOf(this.root1);
        if (indx1 < 0) {
            throw new RuntimeException("ERROR: Corrupted web.xml");
        }

        indx2 = customcontent.indexOf(this.root2, indx1 + this.root1.length());
        if (indx2 < 0 || indx2 >= customcontent.length() - this.root3.length() - 1) {
            throw new RuntimeException("ERROR: Corrupted web.xml");
        }

        indx3 = customcontent.indexOf(this.root3, indx2);
        if (indx3 < 0) {
            throw new RuntimeException("ERROR: Corrupted web.xml");
        }

        customcontent = customcontent.substring(indx2 + this.root2.length(), indx3);

        indx1 = content.indexOf(this.blockStart);
        String targetStr;
        if (indx1 > 0) {
            indx2 = content.indexOf(this.blockEnd, indx1);
            if (indx2 < 0) {
                throw new RuntimeException("ERROR: Corrupted web.xml");
            }
            targetStr = content.substring(indx1, indx2 + this.blockEnd.length());
            customcontent = this.blockStart + "\r\n" + customcontent + "\r\n" + this.blockEnd;
        } else {
            indx1 = content.indexOf(this.servletStr);
            if (indx1 < 0) {
                throw new RuntimeException("ERROR: Corrupted web.xml");
            }
            targetStr = this.servletStr;
            customcontent = "\r\n" + this.blockStart + "\r\n" + customcontent + "\r\n" + this.blockEnd
                    + "\r\n\r\n" + "\t" + this.servletStr;
        }
        content = content.replace(targetStr, customcontent);
        FileUtils.writeStringToFile(webxml, content, ServerConstants.DEFAULT_ENCODING);

    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    }
}

From source file:cat.calidos.morfeu.model.transform.TransformNoValueIntTest.java

@Before
public void setup() throws Exception {

    File inputFile = new File("target/test-classes/test-resources/transform/document4-as-view.json");
    content = FileUtils.readFileToString(inputFile, Config.DEFAULT_CHARSET);

}

From source file:admin.controller.ServletDisplayLayoutHTML.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* ww w  . jav a  2  s. c  om*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
public void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    super.processRequest(request, response);

    response.setContentType("application/json");
    String modelname = request.getParameter("modelname");

    try (PrintWriter out = response.getWriter()) {
        JSONArray json_arr = new JSONArray();
        JSONObject json_ob = new JSONObject();
        File divFile = new File(AppConstants.LAYOUT_HTML_HOME + File.separator + modelname + ".html");
        String layoutHTMLDiv = "";

        if (divFile.exists())
            layoutHTMLDiv = FileUtils.readFileToString(divFile, "UTF-8");
        json_ob.put("div", layoutHTMLDiv);
        String layoutHTMLTable = "";
        File tableFile = new File(
                AppConstants.BASE_HTML_TEMPLATE_UPLOAD_PATH + File.separator + modelname + ".html");
        if (tableFile.exists())
            layoutHTMLTable = FileUtils.readFileToString(tableFile, "UTF-8");
        json_ob.put("table", layoutHTMLTable);

        json_arr.add(json_ob);
        out.write(new Gson().toJson(json_arr));
    } catch (Exception e) {
        logger.log(Level.SEVERE, util.Utility.logMessage(e, "Exception:", e.getMessage()));

    }
}

From source file:com.github.jknack.amd4j.FileResourceLoader.java

@Override
public String load(final ResourceURI uri) throws IOException {
    return FileUtils.readFileToString(toFile(uri), "UTF-8");
}

From source file:au.org.ala.delta.confor.ErrorsTest.java

protected void runAndTest(String path, String directivesFile, String... resultFileName)
        throws Exception, IOException {

    String directivesFileFullPath = FilenameUtils.concat(path, directivesFile);
    CONFOR.main(new String[] { directivesFileFullPath });

    for (String name : resultFileName) {
        File expectedFile = new File(
                FilenameUtils.concat(FilenameUtils.concat(path, "expected_results"), name));
        String expected = FileUtils.readFileToString(expectedFile, "cp1252");

        System.out.println(expected);

        File actualFile = new File(FilenameUtils.concat(path, name));
        String actual = FileUtils.readFileToString(actualFile, "cp1252");

        System.out.print(actual);

        boolean dosEol = expected.contains("\r\n");
        String expectedLineSeparator = "\n";
        if (dosEol) {
            expectedLineSeparator = "\r\n";
        }//  ww w  .  j a va 2 s  .c o  m

        if (!System.getProperty("line.separator").equals(expectedLineSeparator)) {
            expected = expected.replaceAll(expectedLineSeparator, System.getProperty("line.separator"));
        }

        String expectedFilePathSeparator = "/";
        if (!File.separator.equals(expectedFilePathSeparator)) {
            expected = expected.replaceAll(expectedFilePathSeparator, "\\\\");
        }

        for (int i = 0; i < expected.length(); i++) {
            if (expected.charAt(i) != actual.charAt(i)) {
                System.out.println("Difference @ char: " + i + " Expected: " + expected.charAt(i)
                        + (int) expected.charAt(i) + ", Actual: " + actual.charAt(i) + (int) actual.charAt(i));
                break;
            }
        }
        assertEquals(expected.trim(), actual.trim());
    }
}

From source file:com.thoughtworks.go.security.AESCipherProvider.java

private void primeKeyCache() {
    if (cachedKey == null) {
        synchronized (cipherFile.getAbsolutePath().intern()) {
            if (cachedKey == null) {
                try {
                    if (cipherFile.exists()) {
                        cachedKey = decodeHex(FileUtils.readFileToString(cipherFile, UTF_8).trim());
                        return;
                    }//from   ww w .ja v a  2  s  .  co m
                    byte[] newKey = generateKey();
                    FileUtils.writeStringToFile(cipherFile, encodeHexString(newKey), UTF_8);
                    LOGGER.info("AES cipher not found. Creating a new cipher file");
                    cachedKey = newKey;
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

From source file:de.uzk.hki.da.metadata.XsltEDMGeneratorTests.java

@Before
public void setUp() throws IOException {
    String content = FileUtils.readFileToString(edmSourceFile, Charset.forName("UTF-8"));
    edmInputStream = IOUtils.toInputStream(content, "utf-8");
}

From source file:com.intuit.karate.cucumber.FeatureReuseTest.java

@Test
public void testFailureInCalledShouldFailTest() throws Exception {
    String reportPath = "target/fail.xml";
    File file = new File("src/test/java/com/intuit/karate/cucumber/caller.feature");
    CucumberRunner runner = new CucumberRunner(file);
    KarateJunitFormatter formatter = new KarateJunitFormatter(file.getPath(), reportPath);
    runner.run(formatter);/*from   w w w.  j  a  va 2 s  . c  o  m*/
    formatter.done();
    String contents = FileUtils.readFileToString(new File(reportPath), "utf-8");
    assertTrue(contents.contains("assert evaluated to false: input != 4"));
}

From source file:ch.sbb.releasetrain.config.YamlSerializerTest.java

@Test
public void testSerializeActionConfig() throws Exception {

    ReleaseConfig release = new ReleaseConfig();
    ActionConfig action = new JenkinsActionConfig();
    action.getProperties().put("myProp1", "Hallo Welt1");
    action.getProperties().put("myProp2", "Hallo Welt2");

    ActionConfig action2 = new JenkinsActionConfig();
    action2.getProperties().put("myProp2", "Hallo asxasxsxWelt");

    release.setTyp("demo-release");
    release.getActions().add(action);/*  w w  w.  j a  va 2 s .c  o  m*/
    release.getActions().add(action2);

    File file = new File(testFolder.getRoot(), "action.yml");
    FileUtils.writeStringToFile(file, configSerializer.convertEntry(release), "UTF-8");

    ReleaseConfig release2 = configSerializer.convertEntry(FileUtils.readFileToString(file, "UTF-8"));
    Assert.assertNotNull(release2);
    Assert.assertEquals(release, release2);

}