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:de.tudarmstadt.ukp.dkpro.tc.features.style.TopicWordsFeatureExtractor.java

@Override
public List<Feature> extract(JCas jcas) throws TextClassificationException {
    if (topicFilePath == null || topicFilePath.isEmpty()) {
        System.out.println("Path to word list must be set!");
    }/*from  ww  w  .  j a  v  a  2s.c o  m*/
    List<String> topics = null;
    List<Feature> featList = new ArrayList<Feature>();
    List<String> tokens = JCasUtil.toText(JCasUtil.select(jcas, Token.class));
    try {
        topics = FileUtils.readLines(new File(topicFilePath));
        for (String t : topics) {
            featList.addAll(countWordHits(t, tokens));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return featList;
}

From source file:com.liferay.ide.gradle.core.parser.GradleDependencyUpdater.java

public FindDependenciesVisitor insertDependency(String dependency) throws IOException {
    FindDependenciesVisitor visitor = new FindDependenciesVisitor();

    walkScript(visitor);//from  www. j a  v a  2s .c o  m

    _gradleFileContents = FileUtils.readLines(_file);

    if (!dependency.startsWith("\t")) {
        dependency = "\t" + dependency;
    }

    if (visitor.getDependenceLineNum() == -1) {
        _gradleFileContents.add("");
        _gradleFileContents.add("dependencies {");
        _gradleFileContents.add(dependency);
        _gradleFileContents.add("}");
    } else {
        if (visitor.getColumnNum() != -1) {
            _gradleFileContents = Files.readAllLines(Paths.get(_file.toURI()), StandardCharsets.UTF_8);

            StringBuilder builder = new StringBuilder(
                    _gradleFileContents.get(visitor.getDependenceLineNum() - 1));

            builder.insert(visitor.getColumnNum() - 2, "\n" + dependency + "\n");
            String dep = builder.toString();

            if (CoreUtil.isWindows()) {
                dep.replace("\n", "\r\n");
            } else if (CoreUtil.isMac()) {
                dep.replace("\n", "\r");
            }

            _gradleFileContents.remove(visitor.getDependenceLineNum() - 1);
            _gradleFileContents.add(visitor.getDependenceLineNum() - 1, dep);
        } else {
            _gradleFileContents.add(visitor.getDependenceLineNum() - 1, dependency);
        }
    }

    return visitor;
}

From source file:edu.ku.brc.util.HelpIndexer.java

/**
 * /*from  ww w.  j a  v a 2  s  . c om*/
 */
@SuppressWarnings("unchecked")
public void indexIt() {
    if (!jhm.exists()) {
        System.out.println("jhm file not found.");
        return;
    }
    if (!topDir.isDirectory()) {
        System.out.println("directory does not exist.");
        return;
    }
    try {
        mapLines = FileUtils.readLines(jhm);
    } catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(HelpIndexer.class, ex);
        System.out.println("Error reading map file: " + ex.getMessage());
        return;
    }

    Vector<String> lines = new Vector<String>();
    String[] ext = { "html" };
    Iterator<File> helpFiles = FileUtils.iterateFiles(topDir, ext, true);
    while (helpFiles.hasNext()) {
        File file = helpFiles.next();
        System.out.println("Processing " + file.getName());
        processFile(file, lines);
    }

    System.out.println();
    System.out.println("all done.");

    File outFile = new File(outFileName);
    try {

        //add lines to top of file
        lines.insertElementAt("<?xml version='1.0' encoding='ISO-8859-1'  ?>", 0);
        lines.insertElementAt("<!DOCTYPE index", 1);
        lines.insertElementAt("  PUBLIC \"-//Sun Microsystems Inc.//DTD JavaHelp Index Version 1.0//EN\"", 2);
        lines.insertElementAt("         \"http://java.sun.com/products/javahelp/index_1_0.dtd\">", 3);
        lines.insertElementAt("<index version=\"1.0\">", 4);
        lines.insertElementAt(" ", 5);

        //and bottom...
        lines.add(" ");
        lines.add("</index>");

        FileUtils.writeLines(outFile, lines);
    } catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(HelpIndexer.class, ex);
        System.out.println("error writing output file: " + ex.getMessage());
    }
}

From source file:acromusashi.stream.ml.common.spout.TextReadBatchSpout.java

/**
 * {@inheritDoc}// w  w  w  .  ja  va 2 s . c om
 */
@SuppressWarnings({ "rawtypes" })
@Override
public void open(Map conf, TopologyContext context) {
    this.taskIndex = context.getThisTaskIndex();
    this.fileName = this.baseFileName + "_" + this.taskIndex;
    File targetFile = new File(this.dataFilePath, this.fileName);
    try {
        this.fileContents = FileUtils.readLines(targetFile);
        this.fileContentsSize = this.fileContents.size();
    } catch (IOException ex) {
        // ??????????
        throw new RuntimeException(ex);
    }
}

From source file:com.gargoylesoftware.htmlunit.general.HostExtractor.java

private static void ensure(final File file, final Set<String> set) throws IOException {
    final Set<String> unusedNames = new HashSet<>(set);
    final List<String> lines = FileUtils.readLines(file);
    for (final String line : lines) {
        for (final Iterator<String> it = unusedNames.iterator(); it.hasNext();) {
            if (line.contains("(\"" + it.next() + "\")")) {
                it.remove();//from  ww  w. j  a  v  a2s .c o m
            }
        }
    }
    unusedNames.remove("this");
    unusedNames.remove("Boolean");
    unusedNames.remove("null");

    if (!unusedNames.isEmpty()) {
        for (final String name : unusedNames) {
            if (name.contains(" ")) {
                continue;
            }
            System.out.println("");
            System.out.println("    /**");
            System.out.println("     * @throws Exception if the test fails");
            System.out.println("     */");
            System.out.println("    @Test");
            System.out.println("    @Alerts(\"exception\")");
            String methodName = name;
            for (final String prefix : PREFIXES_) {
                if (methodName.startsWith(prefix)) {
                    methodName = prefix.toLowerCase(Locale.ROOT) + methodName.substring(prefix.length());
                    break;
                }
            }
            if (Character.isUpperCase(methodName.charAt(0))) {
                methodName = Character.toLowerCase(methodName.charAt(0)) + methodName.substring(1);
            }
            methodName = methodName.replace(".", "_");
            System.out.println("    public void " + methodName + "() throws Exception {");
            System.out.println("        test(\"" + name + "\");");
            System.out.println("    }");
        }
    }
    for (final String name : unusedNames) {
        if (name.contains(" ")) {
            System.out.println("Ignoring: " + name);
        }
    }
}

From source file:com.igormaznitsa.mindmap.plugins.importers.Text2MindMapImporter.java

@Override
@Nullable/*from  w  ww  .  ja va  2s.  c  o m*/
public MindMap doImport(@Nonnull final MindMapPanel panel, @Nonnull final DialogProvider dialogProvider,
        @Nullable final Topic actionTopic, @Nonnull @MustNotContainNull final Topic[] selectedTopics)
        throws Exception {
    final File file = this.selectFileForExtension(panel,
            Texts.getString("MMDImporters.Text2MindMap.openDialogTitle"), "txt", "text files (.TXT)",
            Texts.getString("MMDImporters.ApproveImport"));
    MindMap result = null;
    if (file != null) {
        final List<String> lines = FileUtils.readLines(file);
        result = makeFromLines(lines, panel.getModel().getController());
    }
    return result;
}

From source file:edu.cornell.med.icb.goby.modes.TestReadQualityStatsMode.java

@Test
public void testHasStatsWithSampling() throws IOException {
    final ReadQualityStatsMode readQuality = new ReadQualityStatsMode();
    readQuality.setSampleFraction(0.5d); // Keep about half of the reads
    readQuality.addInputFile(new File("test-data/compact-reads/five-with-quality.compact-reads"));
    final File outputFile = new File("test-results/five-with-quality.compact-reads.read-qual-stats.tsv");
    readQuality.setOutputFile(outputFile);
    readQuality.execute();/*from ww  w. j  a va2 s  .co m*/
    assertTrue("All reads should have been observed", readQuality.getNumberOfObservedReads() < 5);
    assertTrue("No reads should have been skipped", readQuality.getNumberOfSkippedReads() > 0);
    final List lines = FileUtils.readLines(outputFile);
    assertEquals("Wrong number of stats lines in output file", 39, lines.size());
}

From source file:ee.ria.xroad.common.conf.globalconf.DownloadedFiles.java

void load() throws Exception {
    log.trace("load()");

    File file = confFile.toFile();
    if (file.exists()) {
        oldList.addAll(FileUtils.readLines(file));
    }//from  ww w  .  ja  v  a2 s  .c  om
}

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

/**
 * Generate sdrf for unused rows after splitting by a couple data files.
 */// w  w w . ja  va 2  s .co m
@Test
public void testSplitByUnusedLines() throws IOException {
    int numReferences = 0;
    for (String curLine : sdrfLines) {
        if (curLine.contains(dataFile1.getName()) || curLine.contains(dataFile2.getName())) {
            numReferences++;
        }
    }
    SdrfSplitter splitter = new SdrfSplitter(new JavaIOFileRef(sdrf));
    splitter.splitByDataFile(dataFile1);
    splitter.splitByDataFile(dataFile2);
    FileRef unused = splitter.splitByUnusedLines();

    @SuppressWarnings("unchecked")
    List<String> splitLines = FileUtils.readLines(unused.getAsFile());
    assertEquals(sdrfLines.size() - numReferences, splitLines.size());
    assertEquals(sdrfLines.get(0), splitLines.get(0));
}

From source file:com.linkedin.pinot.index.persist.AvroDataPublisherTest.java

@Test
public void TestReadAvro() throws Exception {

    final String filePath = TestUtils
            .getFileFromResourceUrl(getClass().getClassLoader().getResource(AVRO_DATA));
    final String jsonPath = TestUtils
            .getFileFromResourceUrl(getClass().getClassLoader().getResource(JSON_DATA));

    Schema schema = new Schema.SchemaBuilder().addSingleValueDimension("column3", DataType.STRING)
            .addSingleValueDimension("column2", DataType.STRING).build();

    final SegmentGeneratorConfig config = new SegmentGeneratorConfig(schema);
    config.setFormat(FileFormat.AVRO);/* w  ww  . j  a  v  a2 s . co  m*/
    config.setInputFilePath(filePath);

    config.setSegmentVersion(SegmentVersion.v1);

    AvroRecordReader avroDataPublisher = (AvroRecordReader) RecordReaderFactory.get(config);

    int cnt = 0;
    for (String line : FileUtils.readLines(new File(jsonPath))) {

        JSONObject obj = new JSONObject(line);
        if (avroDataPublisher.hasNext()) {
            GenericRow recordRow = avroDataPublisher.next();

            for (String column : recordRow.getFieldNames()) {
                String valueFromJson = obj.get(column).toString();
                String valueFromAvro = recordRow.getValue(column).toString();
                if (cnt > 1) {
                    Assert.assertEquals(valueFromJson, valueFromAvro);
                }
            }
        }
        cnt++;
    }
    Assert.assertEquals(cnt, 10001);
}