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:com.contentful.generator.lib.TestUtils.java

public static String readTestResource(String fileName) throws IOException {
    return FileUtils.readFileToString(new File("src/test/resources/" + fileName), "UTF-8");
}

From source file:jetbrick.tools.chm.reader.AnchorNameManager.java

public static void addAnchor(File file, String encoding) throws IOException {
    String html = FileUtils.readFileToString(file, encoding);
    html = html.replace('$', '\uFFE5');

    Pattern p = Config.style.getAnchorNameRegex();
    Matcher m = p.matcher(html);// w w w.  j  a va2 s  .  c  o m

    int findCount = 0;
    StringBuffer sb = new StringBuffer();
    while (m.find()) {
        findCount++;
        String anchor = m.group(1);
        // String oldAnchor = m.group();
        String oldAnchor = "<A HH=\"1\" NAME=\"" + anchor + "\">";
        String newAnchor = "<A HH=\"1\" NAME=\"" + getNewAnchorName(anchor) + "\"></A>";
        m.appendReplacement(sb, newAnchor + oldAnchor);
    }
    m.appendTail(sb);

    System.out.println("addAnchor(" + findCount + ") : " + file);

    if (findCount > 0) {
        html = sb.toString().replace('\uFFE5', '$');
        FileUtils.writeStringToFile(file, html, encoding);
    }
    html = null;
}

From source file:net.duckling.falcon.xss.JSONConfig.java

public static Whitelist parse(String filename) throws IOException, ParseException {
    String jsonString = FileUtils.readFileToString(new File(filename), "UTF-8");
    JSONParser parser = new JSONParser();
    Object obj = parser.parse(jsonString);
    if (obj instanceof JSONObject) {
        Whitelist whitelist = new Whitelist();
        JSONObject config = (JSONObject) obj;
        addTags(whitelist, config);/*from w ww .  jav  a 2 s. c  om*/
        addProtocols(whitelist, config);
        return whitelist;
    }
    return Whitelist.none();
}

From source file:com.wavemaker.commons.util.WMFileUtils.java

public static String readFileToString(File file) throws IOException {
    return FileUtils.readFileToString(file, UTF_8_ENCODING);
}

From source file:com.thoughtworks.go.config.ConfigMigrator.java

public static GoConfigMigration migrate(final File configFile) {
    ConfigElementImplementationRegistry registry = ConfigElementImplementationRegistryMother.withNoPlugins();
    String content = "";
    try {//from  w ww.  ja  v  a 2s  .  c o m
        content = FileUtils.readFileToString(configFile, UTF_8);
    } catch (IOException e1) {
    }

    GoConfigMigration upgrader = new GoConfigMigration(new TimeProvider(), registry);
    //TODO: LYH & GL GoConfigMigration should be able to handle stream instead of binding to file
    String upgradedContent = upgrader.upgradeIfNecessary(content);
    try {
        FileUtils.writeStringToFile(configFile, upgradedContent, UTF_8);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return upgrader;
}

From source file:ee.ioc.phon.android.inimesed.MyFileUtils.java

public static String loadFile(File f) throws IOException {
    return FileUtils.readFileToString(f, "UTF8");
}

From source file:by.heap.remark.util.TestUtils.java

/**
 * Reads a resource into a string./*from   w ww .  j  av a 2  s  . co m*/
 * @param path Path to resource
 * @return String contents of resource
 */
public static String readResourceToString(String path) {
    String result;
    try {
        URL u = StringUtils.class.getResource(path);
        if (u == null) {
            throw new Exception("Resource not found");
        }
        File f = FileUtils.toFile(u);
        if (!f.isFile()) {
            throw new Exception("Resource file does not exist or is not a file.");
        }
        result = FileUtils.readFileToString(f, "UTF-8");
        if (result == null) {
            throw new Exception("Error reading resource file.");
        }
    } catch (Exception e) {
        e.printStackTrace();
        result = "UNABLE TO LOAD RESOURCE " + path + ": " + e.getMessage();
    }
    return result;
}

From source file:de.pixida.logtest.automatondefinitions.SomeTestAutomaton.java

static JSONObject getRawConfigJson() {
    try {//from   w ww.j  ava  2s  . c om
        return new JSONObject(FileUtils.readFileToString(getTestAutomatonFileResource(""),
                JsonAutomatonDefinition.EXPECTED_CHARSET));
    } catch (JSONException | IOException e) {
        throw new RuntimeException("Test automaton definition file not found or invalid syntax!?", e);
    }
}

From source file:com.blackducksoftware.integration.hub.detect.detector.clang.CompileCommandsJsonFile.java

public static List<CompileCommand> parseJsonCompilationDatabaseFile(final Gson gson,
        final File compileCommandsJsonFile) throws IOException {
    final String compileCommandsJson = FileUtils.readFileToString(compileCommandsJsonFile,
            StandardCharsets.UTF_8);
    final CompileCommandJsonData[] compileCommands = gson.fromJson(compileCommandsJson,
            CompileCommandJsonData[].class);
    return Arrays.stream(compileCommands).map(rawCommand -> new CompileCommand(rawCommand))
            .collect(Collectors.toList());
}

From source file:is.iclt.icenlp.core.utils.FileOperations.java

/**
 * Reads a file to a String. Complains to system out and prints stack trace
 * if an IOException occurs./*  w ww. j  a v a 2  s  .  co  m*/
 * @param filename Name of the file to be read.
 * @return The content of the file as a String.
 */

public static String fileToString(String filename) {
    String contents = null;
    try {
        //contents = FileUtils.readFileToString(new File(filename));
        contents = FileUtils.readFileToString(new File(filename), FileEncoding.ENCODING);
    } catch (IOException ex) {
        System.out.println("Could not read file '" + filename + "'!");
        ex.printStackTrace();
    }
    return contents;
}