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:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step11GoldDataStatistics.java

/**
 * (1) Plain text with 4 columns: (1) the rank of the document in the list
 * (2) average agreement rate over queries (3) standard deviation of
 * agreement rate over queries. (4) average length of the document in the
 * rank./*from  w ww .jav a  2 s .com*/
 */
public static void statistics1(File inputDir, File outputDir) throws Exception {
    SortedMap<Integer, DescriptiveStatistics> mapDocumentRankObservedAgreement = new TreeMap<>();
    SortedMap<Integer, DescriptiveStatistics> mapDocumentRankDocLength = new TreeMap<>();

    // iterate over query containers
    for (File f : FileUtils.listFiles(inputDir, new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));

        for (QueryResultContainer.SingleRankedResult rankedResult : queryResultContainer.rankedResults) {
            // add new entries
            if (!mapDocumentRankObservedAgreement.containsKey(rankedResult.rank)) {
                mapDocumentRankObservedAgreement.put(rankedResult.rank, new DescriptiveStatistics());
            }
            if (!mapDocumentRankDocLength.containsKey(rankedResult.rank)) {
                mapDocumentRankDocLength.put(rankedResult.rank, new DescriptiveStatistics());
            }

            Double observedAgreement = rankedResult.observedAgreement;

            if (observedAgreement == null) {
                System.err
                        .println("Observed agreement is null; " + f.getName() + ", " + rankedResult.clueWebID);
            } else {
                // update value
                mapDocumentRankObservedAgreement.get(rankedResult.rank).addValue(observedAgreement);
                mapDocumentRankDocLength.get(rankedResult.rank).addValue(rankedResult.plainText.length());
            }
        }
    }

    PrintWriter pw = new PrintWriter(new FileWriter(new File(outputDir, "stats1.csv")));
    for (Map.Entry<Integer, DescriptiveStatistics> entry : mapDocumentRankObservedAgreement.entrySet()) {
        pw.printf(Locale.ENGLISH, "%d\t%.4f\t%.4f\t%.4f\t%.4f%n", entry.getKey(), entry.getValue().getMean(),
                entry.getValue().getStandardDeviation(), mapDocumentRankDocLength.get(entry.getKey()).getMean(),
                mapDocumentRankDocLength.get(entry.getKey()).getStandardDeviation());
    }
    pw.close();
}

From source file:com.vilt.minium.app.controller.TextFileResult.java

public TextFileResult(File file) throws IOException {
    filePath = file.getAbsolutePath();
    content = FileUtils.readFileToString(file, Charsets.UTF_8.name());
}

From source file:de.tudarmstadt.ukp.dkpro.core.examples.example1.NameAnnotationPipelineTest.java

@Test
public void test() throws Exception {
    NameAnnotationPipeline.main(new String[] {});
    assertEquals(FileUtils
            .readFileToString(new File("src/test/resources/reference/example1/output.txt"), "UTF-8").trim(),
            FileUtils.readFileToString(new File("target/output.txt"), "UTF-8").trim());
}

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

public static FeatureWrapper fromFile(File file, ClassLoader classLoader) {
    try {//  w  w  w.  ja v a 2 s . c o  m
        String text = FileUtils.readFileToString(file, "utf-8");
        return new FeatureWrapper(text, ScriptEnv.init(file.getParentFile(), classLoader));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.micromata.genome.util.runtime.CwdTest.java

@Test
public void testCwd() {
    String oldpwd = System.getProperty("user.dir");
    try {//w w w .ja  va2s . c  o  m
        File curFile = new File("pom.xml");
        String pom1 = FileUtils.readFileToString(curFile, Charset.defaultCharset());
        // Ausgabe: ./pom.xml: C:\Users\roger\d\micromata\genome\genome-commons\pom.xml
        System.out.println("./pom.xml: " + curFile.getAbsolutePath());
        File parentDir = new File("./..");
        // setze user dir auf parent file
        System.setProperty("user.dir", parentDir.getCanonicalFile().getAbsolutePath());
        File pafile = new File("pom.xml");
        // das gibt den namen parent pom aus!!!!
        // Ausgabe: new cwd: (..)./pom.xml: C:\Users\roger\d\micromata\genome\pom.xml
        System.out.println("new cwd: (..)./pom.xml: " + pafile.getAbsolutePath());
        // !!!!!!
        // das liest trotzdem das pom1 aus!!!
        String pom2 = FileUtils.readFileToString(pafile, Charset.defaultCharset());
        Assert.assertEquals(pom1, pom2);
        // das fixt das: 
        File pafile2 = pafile.getAbsoluteFile();
        String pom3 = FileUtils.readFileToString(pafile2, Charset.defaultCharset());
        // jetzt tatsaechlich das parent pom
        Assert.assertNotEquals(pom1, pom3);
    } catch (IOException ex) {
        ex.printStackTrace();
        ;
    } finally {
        System.setProperty("user.dir", oldpwd);
    }
}

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

public static String migrate(String configXml) throws IOException {
    File tempFile = TestFileUtil.createTempFile("cruise-config.xml");
    FileUtils.writeStringToFile(tempFile, configXml, UTF_8);
    migrate(tempFile);//from w ww  . j a va  2 s. co  m
    String newConfigXml = FileUtils.readFileToString(tempFile, UTF_8);
    tempFile.delete();
    return newConfigXml;
}

From source file:com.app.framework.web.mvc.ActionMap.java

public static ActionMap Init(ServletRequest request, ServletResponse response) throws IOException {
    ActionMap actMap = null;//ww  w  .jav a 2  s.  co  m
    HttpServletRequest req = ((HttpServletRequest) request);
    String s1 = req.getContextPath();
    String s2 = req.getRequestURI();
    String s3 = req.getRequestURL().toString();
    String fullUrl = getFullURL(req).toLowerCase();
    if (fullUrl.contains(".css") || fullUrl.contains(".js") || fullUrl.contains(".html")
            || fullUrl.contains(".jpg") || fullUrl.contains(".png") || fullUrl.contains(".gif")
            || fullUrl.contains(".icon")) {
        return null;
    }
    Gson g = new Gson();
    String requestedResource = s2.replace(s1 + "/", "");
    String[] urlParts = requestedResource.split("/");
    if (urlParts != null && urlParts.length >= 2) {
        String controller = urlParts[0];
        String action = urlParts[1];

        String jsonFilePath = req.getServletContext().getRealPath("/WEB-INF/action-map.json");

        String json = FileUtils.readFileToString(new File(jsonFilePath), "utf-8");

        Type listType = new TypeToken<Map<String, ControllerInfo>>() {
        }.getType();
        Map<String, ControllerInfo> map = g.fromJson(json, listType);

        String method = req.getMethod();
        if (map.containsKey(controller)) {
            actMap = new ActionMap();
            ControllerInfo cInfo = map.get(controller);
            ActionInfo mInfo = cInfo.getActions().get(action).get(method);
            actMap.setController(cInfo.getControllerClassName());
            actMap.setAction(mInfo.getMethodName());
            actMap.setModel(mInfo.getModelClassName());
        }
    }
    return actMap;
}

From source file:com.javacreed.examples.io.RafHelperTest.java

@Test
public void test() throws Exception {
    final String data = FileUtils
            .readFileToString(new File(getClass().getResource("/samples/Cologne.txt").toURI()), "UTF-8");

    final File file = RafHelperTest.createSampleFile(data);

    try (RafHelper helper = new RafHelper(file, Mode.READ_WRITE)) {
        helper.lock();/*from  w  w  w. j  a va 2  s .  co m*/
        Assert.assertEquals(data, helper.read("UTF-8"));
        helper.clear();
        Assert.assertEquals("", helper.read("UTF-8"));
    }
}

From source file:de.tudarmstadt.ukp.experiments.dip.wp1.documents.Step9AgreementCollector.java

@SuppressWarnings("unchecked")
public static void computeObservedAgreement(File goldDataFolder, File outputDir) throws Exception {
    // iterate over query containers
    for (File f : FileUtils.listFiles(goldDataFolder, new String[] { "xml" }, false)) {
        QueryResultContainer queryResultContainer = QueryResultContainer
                .fromXML(FileUtils.readFileToString(f, "utf-8"));

        for (QueryResultContainer.SingleRankedResult rankedResult : queryResultContainer.rankedResults) {

            // only non-empty and annotated results
            // No annotations found for document: clueWebID: clueweb12-1407wb-22-10643, queryID: 1006
            // <clueWebID>clueweb12-1407wb-22-10643</clueWebID>
            // <score>5.93809186</score>
            // <additionalInfo>indri</additionalInfo>
            // <plainText></plainText>

            if (rankedResult.plainText != null && !rankedResult.plainText.isEmpty()) {
                if (rankedResult.mTurkRelevanceVotes.isEmpty()) {
                    //                        throw new IllegalStateException("No annotations found for document: "
                    System.err.println("No annotations found for document: " + "clueWebID: "
                            + rankedResult.clueWebID + ", queryID: " + queryResultContainer.qID);
                } else {

                    // first, get all the sentence IDs
                    byte[] bytes = new BASE64Decoder()
                            .decodeBuffer(new ByteArrayInputStream(rankedResult.originalXmi.getBytes()));

                    JCas jCas = JCasFactory.createJCas();
                    XmiCasDeserializer.deserialize(new ByteArrayInputStream(bytes), jCas.getCas());

                    // for each sentence, we'll collect all its annotations
                    TreeMap<Integer, SortedMap<String, String>> sentencesAndRelevanceAnnotations = collectSentenceIDs(
                            jCas);/*from   w  w  w . j ava2 s  . c  o  m*/

                    // now we will the map with mturk annotations
                    // the list of true/false for each sentence will be consistent (the annotator ordering remains)
                    for (QueryResultContainer.MTurkRelevanceVote mTurkRelevanceVote : rankedResult.mTurkRelevanceVotes) {
                        for (QueryResultContainer.SingleSentenceRelevanceVote sentenceRelevanceVote : mTurkRelevanceVote.singleSentenceRelevanceVotes) {

                            String sentenceIDString = sentenceRelevanceVote.sentenceID;
                            if (sentenceIDString == null || sentenceIDString.isEmpty()) {
                                throw new IllegalStateException("Empty sentence ID for turker "
                                        + mTurkRelevanceVote.turkID + ", HIT: " + mTurkRelevanceVote.hitID
                                        + ", clueWebID: " + rankedResult.clueWebID + ", queryID: "
                                        + queryResultContainer.qID);
                            } else {

                                Integer sentenceIDInt = Integer.valueOf(sentenceIDString);
                                String value = sentenceRelevanceVote.relevant;

                                // add to the list

                                // sanity check first
                                if (sentencesAndRelevanceAnnotations.get(sentenceIDInt)
                                        .containsKey(mTurkRelevanceVote.turkID)) {
                                    System.err.println("Annotations for sentence " + sentenceIDInt
                                            + " for turker " + mTurkRelevanceVote.turkID + " are duplicate");
                                }

                                sentencesAndRelevanceAnnotations.get(sentenceIDInt)
                                        .put(mTurkRelevanceVote.turkID, value);
                            }
                        }
                    }

                    //                    for (Map.Entry<Integer, SortedMap<String, String>> entry : sentencesAndRelevanceAnnotations
                    //                            .entrySet()) {
                    //                        System.out.println(entry.getKey() + ": " + entry.getValue());
                    //                    }

                    // we collect only the "clean" ones
                    Map<Integer, SortedMap<String, String>> cleanSentencesAndRelevanceAnnotations = new HashMap<>();

                    // sanity check -- all sentences are covered with the same number of annotations
                    for (Map.Entry<Integer, SortedMap<String, String>> entry : sentencesAndRelevanceAnnotations
                            .entrySet()) {
                        SortedMap<String, String> singleSentenceAnnotations = entry.getValue();

                        // remove empty sentences
                        if (singleSentenceAnnotations.values().isEmpty()) {
                            //                                throw new IllegalStateException(
                            System.err.println("Empty annotations for sentence, " + "sentenceID: "
                                    + entry.getKey() + ", " + "clueWebID: " + rankedResult.clueWebID
                                    + ", queryID: " + queryResultContainer.qID + "; number of assignments: "
                                    + singleSentenceAnnotations.values().size() + ", expected: "
                                    + NUMBER_OF_TURKERS_PER_HIT + ". Sentence will be skipped in evaluation");
                        } else if (singleSentenceAnnotations.values().size() != NUMBER_OF_TURKERS_PER_HIT) {
                            System.err.println("Inconsistent annotations for sentences, " + "sentenceID: "
                                    + entry.getKey() + ", " + "clueWebID: " + rankedResult.clueWebID
                                    + ", queryID: " + queryResultContainer.qID + "; number of assignments: "
                                    + singleSentenceAnnotations.values().size() + ", expected: "
                                    + NUMBER_OF_TURKERS_PER_HIT + ". Sentence will be skipped in evaluation");
                        } else {
                            cleanSentencesAndRelevanceAnnotations.put(entry.getKey(), entry.getValue());
                        }
                    }

                    // fill the annotation study

                    CodingAnnotationStudy study = new CodingAnnotationStudy(NUMBER_OF_TURKERS_PER_HIT);
                    study.addCategory("true");
                    study.addCategory("false");

                    for (SortedMap<String, String> singleSentenceAnnotations : cleanSentencesAndRelevanceAnnotations
                            .values()) {
                        // only non-empty sentences
                        Collection<String> values = singleSentenceAnnotations.values();
                        if (!values.isEmpty() && values.size() == NUMBER_OF_TURKERS_PER_HIT) {
                            study.addItemAsArray(values.toArray());
                        }

                    }

                    //                    System.out.println(study.getCategories());

                    // Fleiss' multi-pi.
                    FleissKappaAgreement fleissKappaAgreement = new FleissKappaAgreement(study);

                    double percentage;
                    try {
                        percentage = fleissKappaAgreement.calculateObservedAgreement();
                    } catch (InsufficientDataException ex) {
                        // dkpro-statistics feature, see https://github.com/dkpro/dkpro-statistics/issues/24
                        percentage = 1.0;
                    }

                    if (!Double.isNaN(percentage)) {
                        rankedResult.observedAgreement = percentage;
                        //                        System.out.println(sentencesAndRelevanceAnnotations.values());
                    } else {
                        System.err.println("Observed agreement is NaN.");
                    }
                }
            }
        }

        // and save the query to output dir
        File outputFile = new File(outputDir, queryResultContainer.qID + ".xml");
        FileUtils.writeStringToFile(outputFile, queryResultContainer.toXML(), "utf-8");
        System.out.println("Finished " + outputFile);
    }
}

From source file:cz.incad.vdkcommon.VDKJobData.java

public void load() throws Exception {
    File fdef = FileUtils.toFile(Options.class.getResource("/cz/incad/vdkcommon/job.json"));
    String json = FileUtils.readFileToString(fdef, "UTF-8");
    opts = new JSONObject(json);

    File f = new File(this.configFile);
    this.configDir = f.getParent();
    this.configSimpleName = f.getName().split("\\.")[0];
    this.statusFile = this.configDir + File.separator + "status" + File.separator + this.configSimpleName
            + ".status";
    if (f.exists() && f.canRead()) {
        json = FileUtils.readFileToString(f, "UTF-8");
        JSONObject confCustom = new JSONObject(json);
        Iterator keys = confCustom.keys();
        while (keys.hasNext()) {
            String key = (String) keys.next();
            opts.put(key, confCustom.get(key));
        }/*from   w w  w.  j  a v a 2s  . co  m*/
    }

    Iterator keys = runtimeOptions.keys();
    while (keys.hasNext()) {
        String key = (String) keys.next();
        opts.put(key, runtimeOptions.get(key));
    }
    logger.info("VDKJobData loaded");
}