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

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

Introduction

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

Prototype

public static List readLines(File file) throws IOException 

Source Link

Document

Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.

Usage

From source file:au.org.ala.spatial.util.RecordsSmall.java

public RecordsSmall(String dir) throws IOException {
    this.filename = dir + File.separator;

    //look for a small file
    File smallFile = new File(filename + "records.csv.small.species");

    if (!smallFile.exists() && new File(filename + "records.csv").exists()) {
        try {//from   w  ww  .  j  ava 2  s. co  m
            makeSmallFile(filename);
            makeUniquePoints();
        } catch (Exception e) {
            logger.error("failed to make small records files", e);
        }
    }

    //read species
    if (smallFile.exists()) {
        try {
            //open points and pointsToSpecies
            points = new RandomAccessFile(filename + "records.csv.small.points", "r");
            pointsToSpecies = new RandomAccessFile(filename + "records.csv.small.pointsToSpecies", "r");
            maxPoints = new File(filename + "records.csv.small.pointsToSpecies").length() / 4;
            pointsDis = new DataInputStream(
                    new BufferedInputStream(new FileInputStream(filename + "records.csv.small.points")));
            pointsToSpeciesDis = new DataInputStream(new BufferedInputStream(
                    new FileInputStream(filename + "records.csv.small.pointsToSpecies")));

            lsids = FileUtils.readLines(new File(filename + "records.csv.small.species"));

            getUniquePointsAll();
        } catch (Exception e) {
            logger.error("failed to open small records file", e);
        }
    }
}

From source file:de.tudarmstadt.ukp.similarity.experiments.coling2012.util.WordIdfValuesGenerator.java

@SuppressWarnings("unchecked")
public static void computeIdfScores(Dataset dataset) throws Exception {
    File outputFile = new File(UTILS_DIR + "/word-idf/" + dataset.toString() + ".txt");

    System.out.println("Computing word idf values");

    if (outputFile.exists()) {
        System.out.println(" - skipping, already exists");
    } else {//from   ww  w.j av a 2  s .  com
        System.out.println(" - this may take a while...");

        CollectionReader reader = ColingUtils.getCollectionReader(dataset);

        // Tokenization
        AnalysisEngineDescription seg = createPrimitiveDescription(BreakIteratorSegmenter.class);
        AggregateBuilder builder = new AggregateBuilder();
        builder.add(seg, CombinationReader.INITIAL_VIEW, CombinationReader.VIEW_1);
        builder.add(seg, CombinationReader.INITIAL_VIEW, CombinationReader.VIEW_2);
        AnalysisEngine aggr_seg = builder.createAggregate();

        // POS Tagging
        AnalysisEngineDescription pos = createPrimitiveDescription(OpenNlpPosTagger.class,
                OpenNlpPosTagger.PARAM_LANGUAGE, "en");
        builder = new AggregateBuilder();
        builder.add(pos, CombinationReader.INITIAL_VIEW, CombinationReader.VIEW_1);
        builder.add(pos, CombinationReader.INITIAL_VIEW, CombinationReader.VIEW_2);
        AnalysisEngine aggr_pos = builder.createAggregate();

        // Lemmatization
        AnalysisEngineDescription lem = createPrimitiveDescription(StanfordLemmatizer.class);
        builder = new AggregateBuilder();
        builder.add(lem, CombinationReader.INITIAL_VIEW, CombinationReader.VIEW_1);
        builder.add(lem, CombinationReader.INITIAL_VIEW, CombinationReader.VIEW_2);
        AnalysisEngine aggr_lem = builder.createAggregate();

        // Output Writer
        AnalysisEngine writer = createPrimitive(WordIdfValuesGeneratorWriter.class,
                WordIdfValuesGeneratorWriter.PARAM_OUTPUT_FILE, outputFile.getAbsolutePath());

        SimplePipeline.runPipeline(reader, aggr_seg, aggr_pos, aggr_lem, writer);

        // Now we have the text format lemma1###lemma2###...###lemman
        List<String> lines = FileUtils.readLines(outputFile);

        Map<String, Double> idfValues = new HashMap<String, Double>();

        // Build up token representations of texts
        Set<List<String>> docs = new HashSet<List<String>>();

        for (String line : lines) {
            List<String> doc = CollectionUtils.arrayToList(line.split("###"));

            docs.add(doc);
        }

        // Get the shared token list
        Set<String> tokens = new HashSet<String>();
        for (List<String> doc : docs)
            tokens.addAll(doc);

        // Get the idf numbers
        for (String token : tokens) {
            double count = 0;
            for (List<String> doc : docs) {
                if (doc.contains(token))
                    count++;
            }
            idfValues.put(token, count);
        }

        // Compute the idf
        for (String lemma : idfValues.keySet()) {
            double idf = Math.log10(lines.size() / idfValues.get(lemma));
            idfValues.put(lemma, idf);
        }

        // Store persistently
        StringBuilder sb = new StringBuilder();
        for (String key : idfValues.keySet()) {
            sb.append(key + "\t" + idfValues.get(key) + LF);
        }
        FileUtils.writeStringToFile(outputFile, sb.toString());

        System.out.println(" - done");
    }
}

From source file:de.tudarmstadt.ukp.dkpro.tc.examples.io.WeightedTwentyNewsgroupsCorpusReader.java

@Override
public void initialize(UimaContext context) throws ResourceInitializationException {
    super.initialize(context);
    List<String> lines = new ArrayList<String>();
    try {// w  w  w .jav  a2 s  .c  om
        lines = FileUtils.readLines(new File(weightFile));
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
    weights = new HashMap<String, String>();

    for (String l : lines) {
        String[] splitted = l.split(String.valueOf(SEPARATOR_CHAR));
        if (splitted.length == 2)
            weights.put(splitted[0], splitted[1]);
    }
}

From source file:com.kotcrab.vis.editor.util.vis.CrashReporter.java

private void printLog() throws IOException {
    println("--- Log file ---");

    List<String> logLines = FileUtils.readLines(logFile);
    logLines.forEach(this::println);

    println("---------------------------------");
    println();//from   ww  w. j a  va 2  s .c om
}

From source file:edu.ku.brc.specify.dbsupport.cleanuptools.FirstLastVerifier.java

/**
 * @param args/*ww w. j  a v  a2 s. c o m*/
 */
public static void main(String[] args) {
    if (true) {
        testLastNames();
        return;
    }
    FirstLastVerifier flv = new FirstLastVerifier();
    System.out.println(flv.isFirstName("Bill"));
    System.out.println(flv.isLastName("Bill"));

    System.out.println(flv.isFirstName("Johnson"));
    System.out.println(flv.isLastName("Johnson"));

    try {
        if (false) {
            for (String nm : new String[] { "firstnames", "lastnames" }) {
                File file = new File("/Users/rods/Downloads/" + nm + ".txt");
                try {
                    PrintWriter pw = new PrintWriter("/Users/rods/Downloads/" + nm + ".list");
                    for (String line : (List<String>) FileUtils.readLines(file)) {
                        String[] toks = StringUtils.split(line, '\t');
                        if (toks != null && toks.length > 0)
                            pw.println(toks[0]);
                    }
                    pw.close();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }

        Vector<String> lnames = new Vector<String>();
        File file = XMLHelper.getConfigDir("lastnames.list");
        if (false) {
            for (String name : (List<String>) FileUtils.readLines(file)) {
                if (flv.isFirstName(name)) {
                    System.out.println(name + " is first.");
                } else {
                    lnames.add(name);
                }
            }
            Collections.sort(lnames);
            FileUtils.writeLines(file, lnames);
        }

        lnames.clear();
        file = XMLHelper.getConfigDir("firstnames.list");
        for (String name : (List<String>) FileUtils.readLines(file)) {
            if (flv.isLastName(name)) {
                System.out.println(name + " is first.");
            } else {
                lnames.add(name);
            }
        }
        Collections.sort(lnames);
        //FileUtils.writeLines(file, lnames);

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:edu.ku.brc.specify.dbsupport.cleanuptools.FirstLastVerifier.java

/**
 * @param isLastName/*  w  w  w .j a  v a 2  s. c  o m*/
 */
protected void buildFromFile(final boolean isLastName) {
    File file = XMLHelper
            .getConfigDir("nameresources" + File.separator + (isLastName ? "last" : "first") + "names.list");
    try {
        for (String name : (List<String>) FileUtils.readLines(file)) {
            Document doc = new Document();
            String fld = isLastName ? "last" : "first";
            doc.add(new Field(fld, name, Field.Store.YES, Field.Index.ANALYZED));

            try {
                writer.addDocument(doc);
            } catch (CorruptIndexException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:gov.nih.nci.caarray.magetab.splitter.SdrfSplitter.java

private void splitIntoLines() throws IOException {
    @SuppressWarnings("unchecked")
    List<String> inputLines = FileUtils.readLines(file.getAsFile());
    for (String curLine : inputLines) {
        if (curLine != null && !isCommentLine(curLine)) {
            lines.add(curLine);//from  ww  w .ja v a 2s  .  com
        }
    }
    if (lines.isEmpty()) {
        throw new IllegalArgumentException("Could not find header row in sdrf file.  Was it validated?");
    }
}

From source file:it.serverSystem.LogsTest.java

@Test
public void info_log_in_sonar_log_file_when_SQ_is_done_starting() throws IOException {
    List<String> logs = FileUtils.readLines(orchestrator.getServer().getAppLogs());
    String sqIsUpMessage = "SonarQube is up";
    assertThat(logs.stream().filter(str -> str.contains(sqIsUpMessage)).findFirst())
            .describedAs("message is there").isNotEmpty();
    assertThat(logs.get(logs.size() - 1)).describedAs("message is the last line of logs")
            .contains(sqIsUpMessage);//from w w  w.ja  va2  s.c o m
}

From source file:com.antelink.sourcesquare.server.Simulator.java

@Override
public void run() {
    int nbFilesToScan = 101;
    ScanStatus.INSTANCE.setNbFilesToScan(nbFilesToScan);
    ScanStatus.INSTANCE.setNbFilesScanned(0);
    ScanStatus.INSTANCE.setNbOSFilesFound(0);
    ScanStatus.INSTANCE.setQuerying();//from  w  w  w  . ja va 2s. com
    for (int i = 0; i < nbFilesToScan; i++) {
        logger.debug("increasing the count by " + i);
        System.out.println(i);
        ScanStatus.INSTANCE.setNbFilesScanned(i);
        if (i - 100 > 0 && i % 8 == 0) {
            ScanStatus.INSTANCE.setNbOSFilesFound(i - 100);
        }
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    StringBuilder builder = new StringBuilder();
    try {
        List<String> object = FileUtils.readLines(new File("src/test/resources/json.txt"));
        for (String o : object) {
            builder.append(o);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    String deserialize = builder.toString();
    Gson gson = new Gson();
    TreemapNode root = gson.fromJson(deserialize, TreemapNode.class);
    SourceSquareResults result = new SourceSquareResults();
    result.setRootNode(root);
    List<Badge> badges = new ArrayList<Badge>();
    badges.add(Badge.OS_JEDI_KNIGHT);
    badges.add(Badge.OLD);
    result.setBadges(badges);
    result.setNodeLevel(3);
    this.eventBus.fireEvent(new SourceSquareResultsReadyEvent(result));
    ScanStatus.INSTANCE.setComplete();
}

From source file:de.tudarmstadt.ukp.dkpro.core.textnormalizer.casfilter.CasFilter_ImplBaseTest.java

@Test
public void testAnnotationFilterPass() throws UIMAException, IOException {
    String input = "test";
    String expectedFirstLine = "======== CAS 0 begin ==================================";

    CollectionReaderDescription reader = createReaderDescription(StringReader.class,
            StringReader.PARAM_DOCUMENT_TEXT, input, StringReader.PARAM_LANGUAGE, "en");
    AnalysisEngineDescription filter = createEngineDescription(AnnotationBasedFilter.class);
    AnalysisEngineDescription annotator = createEngineDescription(TestAnnotator.class);
    AnalysisEngineDescription writer = createEngineDescription(CasDumpWriter.class,
            CasDumpWriter.PARAM_TARGET_LOCATION, tmpFile);
    AnalysisEngineDescription aggregator = CasFilter_ImplBase.createAggregateBuilderDescription(filter, writer);

    SimplePipeline.runPipeline(reader, annotator, aggregator);

    List<String> output = FileUtils.readLines(tmpFile);
    assertEquals(expectedFirstLine, output.get(0));
    assertEquals(input, output.get(13));
    assertEquals("Sentence", output.get(15));
}