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:ml.shifu.shifu.core.pmml.PMMLTranslatorTest.java

private void compareScore(File test, File control, String scoreName, String sep, Double errorRange)
        throws Exception {
    List<String> testData = FileUtils.readLines(test);
    List<String> controlData = FileUtils.readLines(control);
    String[] testSchema = testData.get(0).trim().split(sep);
    String[] controlSchema = controlData.get(0).trim().split(sep);

    for (int row = 1; row < controlData.size(); row++) {
        Map<String, Object> ctx = new HashMap<String, Object>();
        Map<String, Object> controlCtx = new HashMap<String, Object>();

        String[] testRowValue = testData.get(row).split(sep, testSchema.length);
        for (int index = 0; index < testSchema.length; index++) {
            ctx.put(testSchema[index], testRowValue[index]);
        }/*from www .j av a  2 s  . c om*/
        String[] controlRowValue = controlData.get(row).split(sep, controlSchema.length);

        for (int index = 0; index < controlSchema.length; index++) {
            controlCtx.put(controlSchema[index], controlRowValue[index]);
        }
        Double controlScore = Double.valueOf((String) controlCtx.get(scoreName));
        Double testScore = Double.valueOf((String) ctx.get(scoreName));

        Assert.assertEquals(controlScore, testScore, errorRange);
    }
}

From source file:com.gargoylesoftware.htmlunit.source.BrowserVersionFeaturesSource.java

private void generate(final File dir, final String toSearchFor, final BrowserVersion[] versions)
        throws IOException {
    final File propertiesFolder = new File(root_,
            "src/main/resources/com/gargoylesoftware/htmlunit/javascript/configuration");
    final File featuresFile = new File(root_,
            "src/main/java/com/gargoylesoftware/htmlunit/BrowserVersionFeatures.java");
    for (final File f : dir.listFiles()) {
        if (f.isDirectory()) {
            if (!".svn".equals(f.getName())) {
                generate(f, toSearchFor, versions);
            }/* w w w.  j a  v a 2s.  c  o  m*/
        } else if (!"JavaScriptConfiguration.java".equals(f.getName())) {
            final List<String> lines = FileUtils.readLines(f);
            boolean modified = false;
            for (int i = 0; i < lines.size(); i++) {
                String line = lines.get(i);
                if (line.contains(toSearchFor)) {
                    line = line.replace(toSearchFor,
                            ".hasFeature(BrowserVersionFeatures." + GENERATED_PREFIX + generatedNext_ + ")");
                    lines.set(i, line);
                    modified = true;

                    final List<String> featuresLines = FileUtils.readLines(featuresFile);
                    featuresLines.add(featuresLines.size() - 4, "");
                    featuresLines.add(featuresLines.size() - 4,
                            "    /** Was originally " + toSearchFor + ". */");
                    featuresLines.add(featuresLines.size() - 4,
                            "    " + GENERATED_PREFIX + generatedNext_ + ",");
                    FileUtils.writeLines(featuresFile, featuresLines);

                    for (final File file : propertiesFolder.listFiles(new FileFilter() {
                        public boolean accept(final File pathname) {
                            for (final BrowserVersion version : versions) {
                                if (pathname.getName().equals(version.getNickname() + ".properties")) {
                                    return true;
                                }
                            }
                            return false;
                        }
                    })) {
                        final List<String> features = FileUtils.readLines(file);
                        features.add(GENERATED_PREFIX + generatedNext_);
                        Collections.sort(features);
                        FileUtils.writeLines(file, features);
                    }
                    generatedNext_++;
                }
            }
            if (modified) {
                FileUtils.writeLines(f, lines);
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.io.ditop.DiTopWriterTest.java

@Test
public void testSimple() throws UIMAException, IOException {
    int expectedNDocuments = 2;

    CollectionReaderDescription reader = createReaderDescription(TextReader.class,
            TextReader.PARAM_SOURCE_LOCATION, CAS_DIR, TextReader.PARAM_PATTERNS, CAS_FILE_PATTERN,
            TextReader.PARAM_LANGUAGE, LANGUAGE);
    AnalysisEngineDescription segmenter = createEngineDescription(BreakIteratorSegmenter.class);
    AnalysisEngineDescription inferencer = createEngineDescription(MalletTopicModelInferencer.class,
            MalletTopicModelInferencer.PARAM_MODEL_LOCATION, MODEL_FILE);
    AnalysisEngineDescription ditopwriter = createEngineDescription(DiTopWriter.class,
            DiTopWriter.PARAM_TARGET_LOCATION, TARGET_DITOP, DiTopWriter.PARAM_MODEL_LOCATION, MODEL_FILE,
            DiTopWriter.PARAM_CORPUS_NAME, DITOP_CORPUSNAME);

    SimplePipeline.runPipeline(reader, segmenter, inferencer, ditopwriter);

    /* test whether target files and dirs exist */
    File contentDir = new File(TARGET_DITOP, DITOP_CORPUSNAME + "_" + N_TOPICS);
    File topicsFile = new File(contentDir, "topics.csv");
    File topicTermT15File = new File(contentDir, "topicTerm-T15.txt");
    File topicTermFile = new File(contentDir, "topicTerm.txt");
    File topicTermMatrixFile = new File(contentDir, "topicTermMatrix.txt");

    assertTrue(new File(TARGET_DITOP, "config.all").exists());
    assertTrue(contentDir.isDirectory());
    assertTrue(topicTermT15File.exists());
    assertTrue(topicTermFile.exists());/*from ww w . ja v  a  2  s  .  co m*/
    assertTrue(topicTermMatrixFile.exists());
    assertTrue(topicsFile.exists());

    /* check that file lengths are correct */
    assertEquals(expectedNDocuments + 1, FileUtils.readLines(topicsFile).size());
    assertEquals(N_TOPICS, FileUtils.readLines(topicTermT15File).size());
    assertEquals(N_TOPICS, FileUtils.readLines(topicTermFile).size());
    assertEquals(N_TOPICS, FileUtils.readLines(topicTermMatrixFile).size());
}

From source file:es.uvigo.ei.sing.adops.datatypes.BatchProject.java

public BatchProject(File folder) throws IllegalStateException {
    this.folder = folder;
    this.deleted = false;
    this.running = false;
    this.propertiesFile = new File(this.folder, BatchProject.CONFIGURATION_FILE);
    this.configuration = new Configuration(Configuration.getSystemConfiguration(), this.propertiesFile);

    if (this.hasAbsoluteFastaPaths()) {
        this.fastaFiles = filenamesToFiles(this.configuration.getFastaFile());
    } else {/*from  w w  w.j a  v  a 2  s. c  o  m*/
        final List<File> fastaList = new LinkedList<>();

        try {
            for (String fastaFilename : FileUtils.readLines(new File(this.folder, "fasta.names"))) {
                fastaList.add(new File(this.folder, fastaFilename));
            }

            this.fastaFiles = fastaList.toArray(new File[fastaList.size()]);
        } catch (IOException e) {
            throw new IllegalStateException("Missing 'fasta.names' file", e);
        }
    }

    Arrays.sort(this.fastaFiles);
    this.getOutput(); // Forces output loading
}

From source file:edu.ku.brc.specify.tools.datamodelgenerator.DataDict.java

/**
 * @param query//  w  w  w .  j  a  v a  2s  . c  om
 */
@SuppressWarnings("unchecked")
public void processSpecify5Schema() {
    File file = new File("src/edu/ku/brc/specify/tools/datamodelgenerator/Specify5Tables.txt");
    try {
        TableDef tableDef = null;
        Vector<FieldDef> fields = null;

        boolean processingTable = false;

        for (String line : (List<String>) FileUtils.readLines(file)) {
            if (processingTable) {
                if (line.startsWith(") ON [")) {
                    processingTable = false;
                } else {
                    String[] tokens = StringUtils.split(line, "[]");
                    //System.out.println(line);
                    String notNull = tokens[4];

                    FieldDef fieldDef = new FieldDef(tokens[1], tokens[3], "",
                            notNull != null && notNull.indexOf("NOT NULL") > -1 ? "true" : "false", "");
                    fields.add(fieldDef);
                    tokens = StringUtils.split(line, "()");
                    if (tokens.length > 1) {
                        fieldDef.setLength(tokens[1]);
                    }
                }

            } else if (line.startsWith("CREATE TABLE")) {
                String[] tokens = StringUtils.split(line, "[]");
                tableDef = new TableDef(tokens[3], "");
                sp5Tables.add(tableDef);
                fields = tableDef.getFields();
                processingTable = true;
            }
        }
    } catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataDict.class, ex);
        System.err.println(file.getAbsolutePath());
        ex.printStackTrace();
    } catch (Exception ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(DataDict.class, ex);
        ex.printStackTrace();
    }
}

From source file:com.iyonger.apm.web.service.ScriptValidationService.java

@Override
public String validate(User user, IFileEntry scriptIEntry, boolean useScriptInSVN, String hostString) {
    FileEntry scriptEntry = TypeConvertUtils.cast(scriptIEntry);
    try {//from   ww  w . j a  v  a 2 s. c  o  m
        checkNotNull(scriptEntry, "scriptEntity should be not null");
        checkNotEmpty(scriptEntry.getPath(), "scriptEntity path should be provided");
        if (!useScriptInSVN) {
            checkNotEmpty(scriptEntry.getContent(), "scriptEntity content should be provided");
        }
        checkNotNull(user, "user should be provided");
        // String result = checkSyntaxErrors(scriptEntry.getContent());

        ScriptHandler handler = scriptHandlerFactory.getHandler(scriptEntry);
        if (config.getControllerProperties().getPropertyBoolean(PROP_CONTROLLER_VALIDATION_SYNTAX_CHECK)) {
            String result = handler.checkSyntaxErrors(scriptEntry.getPath(), scriptEntry.getContent());
            LOGGER.info("Perform Syntax Check by {} for {}", user.getUserId(), scriptEntry.getPath());
            if (result != null) {
                return result;
            }
        }
        File scriptDirectory = config.getHome().getScriptDirectory(user);
        FileUtils.deleteDirectory(scriptDirectory);
        Preconditions.checkTrue(scriptDirectory.mkdirs(), "Script directory {} creation is failed.");

        ProcessingResultPrintStream processingResult = new ProcessingResultPrintStream(
                new ByteArrayOutputStream());
        handler.prepareDist(0L, user, scriptEntry, scriptDirectory, config.getControllerProperties(),
                processingResult);
        if (!processingResult.isSuccess()) {
            return new String(processingResult.getLogByteArray());
        }
        File scriptFile = new File(scriptDirectory, FilenameUtils.getName(scriptEntry.getPath()));

        if (useScriptInSVN) {
            fileEntryService.writeContentTo(user, scriptEntry.getPath(), scriptDirectory);
        } else {
            FileUtils.writeStringToFile(scriptFile, scriptEntry.getContent(),
                    StringUtils.defaultIfBlank(scriptEntry.getEncoding(), "UTF-8"));
        }
        File doValidate = localScriptTestDriveService.doValidate(scriptDirectory, scriptFile, new Condition(),
                config.isSecurityEnabled(), hostString, getTimeout());
        List<String> readLines = FileUtils.readLines(doValidate);
        StringBuilder output = new StringBuilder();
        String path = config.getHome().getDirectory().getAbsolutePath();
        for (String each : readLines) {
            if (!each.startsWith("*sys-package-mgr")) {
                each = each.replace(path, "${NGRINDER_HOME}");
                output.append(each).append("\n");
            }
        }
        return output.toString();
    } catch (Exception e) {
        throw processException(e);
    }
}

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

/**
 * linesToAdd are added in sequential order.  This <em>modifies</em> the lines
 * in place.  So to add 2 lines before the header and 1 after, the keys should be
 * (0, 1, 3), because that will add two lines (moving the header down), then add after
 * the header./* w ww. ja v a 2 s  .  c o m*/
 */
private Function<File, File> getTransform(final SortedMap<Integer, String> linesToAdd) {
    Function<File, File> transform = new Function<File, File>() {
        @Override
        public File apply(File input) {
            try {
                @SuppressWarnings("unchecked")
                List<String> lines = FileUtils.readLines(input);
                for (Integer i : linesToAdd.keySet()) {
                    lines.add(i, linesToAdd.get(i));
                }
                File revisedSdrf = File.createTempFile("test", ".sdrf");
                FileUtils.writeLines(revisedSdrf, lines);
                return revisedSdrf;
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
    };
    return transform;
}

From source file:de.nbi.ontology.test.OntologyMatchTest.java

/**
 * Test, if terms are properly match to concept labels. The a list of terms
 * contains a term in each line./* www  .  ja  va 2 s .c o m*/
 * 
 * @param inFile
 *            a list of terms
 * @throws IOException
 */
@SuppressWarnings("unchecked")
@Test(dataProviderClass = TestFileProvider.class, dataProvider = "parentTestFiles", groups = { "functest" })
public void parents(File inFile) throws IOException {
    log.info("Processing " + inFile.getName());
    String basename = FilenameUtils.removeExtension(inFile.getAbsolutePath());
    File outFile = new File(basename + ".out");
    File resFile = new File(basename + ".res");

    List<String> terms = FileUtils.readLines(inFile);
    PrintWriter w = new PrintWriter(new FileWriter(outFile));
    for (String term : terms) {
        OntClass clazz = index.getModel().getOntClass(term);
        log.trace("** matching " + term);
        w.println(index.getParents(clazz));
    }
    w.flush();
    w.close();

    Assert.assertTrue(FileUtils.contentEquals(outFile, resFile));
}

From source file:de.tudarmstadt.ukp.dkpro.core.mallet.lda.io.MalletLdaTopicProportionsWriterTest.java

@Test
public void testMultipleTargets() throws IOException, UIMAException {
    File targetDir = testContext.getTestOutputFolder();
    File expectedFile0 = new File(targetDir, "dummy1.txt.topics");
    File expectedFile1 = new File(targetDir, "dummy2.txt.topics");

    int expectedLines = 1;
    String expectedLine0Regex = "dummy1.txt\t(0\\.[0-9]{4}\\t){9}0\\.[0-9]{4}";
    String expectedLine1Regex = "dummy2.txt\t(0\\.[0-9]{4}\\t){9}0\\.[0-9]{4}";

    File modelFile = new File(testContext.getTestOutputFolder(), "model");
    MalletLdaUtil.trainModel(modelFile);

    CollectionReaderDescription reader = createReaderDescription(TextReader.class,
            TextReader.PARAM_SOURCE_LOCATION, MalletLdaUtil.CAS_DIR, TextReader.PARAM_PATTERNS,
            MalletLdaUtil.CAS_FILE_PATTERN, TextReader.PARAM_LANGUAGE, MalletLdaUtil.LANGUAGE);
    AnalysisEngineDescription segmenter = createEngineDescription(BreakIteratorSegmenter.class);

    AnalysisEngineDescription inferencer = createEngineDescription(MalletLdaTopicModelInferencer.class,
            MalletLdaTopicModelInferencer.PARAM_MODEL_LOCATION, modelFile);

    AnalysisEngineDescription writer = createEngineDescription(MalletLdaTopicProportionsWriter.class,
            MalletLdaTopicProportionsWriter.PARAM_TARGET_LOCATION, targetDir,
            MalletLdaTopicProportionsWriter.PARAM_OVERWRITE, true,
            MalletLdaTopicProportionsWriter.PARAM_SINGULAR_TARGET, false,
            MalletLdaTopicProportionsWriter.PARAM_WRITE_DOCID, true);

    SimplePipeline.runPipeline(reader, segmenter, inferencer, writer);

    assertTrue(expectedFile0.exists());/*from  ww  w . j  av  a  2  s  .c  o  m*/
    assertTrue(expectedFile1.exists());

    List<String> lines = FileUtils.readLines(expectedFile0);
    assertTrue(lines.get(0).matches(expectedLine0Regex));
    assertEquals(expectedLines, lines.size());
    lines = FileUtils.readLines(expectedFile1);
    assertEquals(expectedLines, lines.size());
    assertTrue(lines.get(0).matches(expectedLine1Regex));
}

From source file:com.etas.jenkins.plugins.CreateTextFile.CreateFileBuilderTest.java

@Test
public void testAppendToEndFile() throws InterruptedException, IOException, Exception {

    EnvironmentVariablesNodeProperty prop = new EnvironmentVariablesNodeProperty();
    EnvVars envVars = prop.getEnvVars();
    envVars.put("FILE_NAME", "ExistingFile.txt");
    envVars.put("LINE2", "Line2");
    j.jenkins.getGlobalNodeProperties().add(prop);
    FreeStyleProject project = j.createFreeStyleProject();

    project.getBuildersList().add(new TestBuilder() {

        @Override/*from  w  ww  . ja  v  a 2s.  c  o  m*/
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
                throws InterruptedException, IOException {

            build.getWorkspace().child("ExistingFile.txt").write("LINE1", "UTF-8");

            return true;

        }
    });

    CreateFileBuilder builder = new CreateFileBuilder("${WORKSPACE}" + "/" + "${FILE_NAME}", "${LINE2}",
            "appendToEnd", true);
    project.getBuildersList().add(builder);
    FreeStyleBuild build = project.scheduleBuild2(0).get();
    String filePath = build.getWorkspace().child("ExistingFile.txt").getRemote();

    String firstLine = (String) FileUtils.readLines(new File(filePath)).get(0);
    String secondLine = (String) FileUtils.readLines(new File(filePath)).get(2);
    assertTrue(firstLine.equals("LINE1"));
    assertTrue(secondLine.equals("Line2"));
}