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:gov.fda.open.demo.service.FDADataProxyServiceImpl.java

/**
 * Returns all the drug names that start with give drug name in sorted order
 * /*from   w w  w.ja va  2  s .co  m*/
 * @see gov.fda.open.demo.service.FDADataProxyService#getDrugNames(java.lang.String)
 */
@Loggable(LogLevel.INFO)
@Override
public List<String> getDrugNames(String partialDrugName) {

    try {
        if (drugNames == null) {
            // TOD Add constants
            URL url = Thread.currentThread().getContextClassLoader().getResource("fdadrugnames.txt");
            List<String> lines = FileUtils.readLines(new File(url.toURI()));
            Collections.sort(lines);
            drugNames = lines.toArray(new String[lines.size()]);
        }

        String upperDrugName = partialDrugName.toUpperCase();
        int idx = StringUtil.findFirst(drugNames, upperDrugName);
        List<String> uniqueDrugNames = new ArrayList<String>();
        // If greater than -1 then found something
        if (idx > -1) {
            for (int i = idx; i <= drugNames.length; i++) {
                if (drugNames[i].startsWith(upperDrugName) && uniqueDrugNames.size() < 10) {
                    uniqueDrugNames.add(drugNames[i]);
                } else {
                    break;
                }

            }
        }
        return uniqueDrugNames;
    } catch (IOException | URISyntaxException ie) {
        throw new ApplicationException(ie);
    }

}

From source file:dkpro.similarity.algorithms.lexical.string.CosineSimilarity.java

public CosineSimilarity(WeightingModeIdf modeIdf, NormalizationMode normMode, String idfScoresFile) {
    HashMap<String, Double> idfValues = null;
    if (idfScoresFile != null) {
        idfValues = new HashMap<String, Double>();
        try {/*from w  w  w  .j a  va 2  s.  c  o m*/
            for (String line : FileUtils.readLines(new File(idfScoresFile))) {
                if (line.length() > 0) {
                    String[] cols = line.split("\t");
                    idfValues.put(cols[0], Double.parseDouble(cols[1]));
                }
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        idfValues = null;
    }
    initialize(null, modeIdf, normMode, idfValues);
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.convincingness.svmlib.SVMLibExperimentRunner.java

public Set<SinglePrediction> loadPredictions(File testFile, File modelPredictions) throws IOException {
    List<String> testLines = FileUtils.readLines(testFile);
    List<String> predictionLines = FileUtils.readLines(modelPredictions);

    if (testLines.size() != predictionLines.size()) {
        throw new IllegalStateException("Mismatch in test file size and prediction size. " + "Test file "
                + testFile + " has " + testLines.size() + " lines while " + "predictions " + modelPredictions
                + " has " + predictionLines.size() + " lines");
    }/*from   w w  w .j av a2 s  . c  o m*/

    Set<SinglePrediction> result = new HashSet<SinglePrediction>();

    for (int i = 0; i < testLines.size(); i++) {
        String test = testLines.get(i);
        String id = test.split("#")[1].trim();
        String gold = test.split("\t")[0].trim();
        String prediction = predictionLines.get(i).trim();

        SinglePrediction sp = new SinglePrediction(id, gold, prediction);

        result.add(sp);
    }

    return result;
}

From source file:com.sketchy.server.action.ManageNetworkSettings.java

private NetworkInfo readNetworkInfo() throws Exception {
    NetworkInfo networkInfo = new NetworkInfo();

    List<String> lines = FileUtils.readLines(WPA_SUPPLICANT_FILE);

    for (String line : lines) {
        String ssid = StringUtils.substringAfter(line, "ssid=");
        String psk = StringUtils.substringAfter(line, "psk=");
        if (StringUtils.isNotBlank(ssid)) {
            networkInfo.ssid = StringUtils.substringBetween(ssid, "\"", "\"");
        }/*  ww w  .j av  a2  s. c  om*/
        if (StringUtils.isNotBlank(psk)) {
            networkInfo.password = StringUtils.substringBetween(psk, "\"", "\"");
        }
    }
    return networkInfo;
}

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

@Test
public void test() throws UIMAException, IOException {
    File targetFile = new File(testContext.getTestOutputFolder(), "topics.txt");
    File modelFile = new File(testContext.getTestOutputFolder(), "model");
    trainModel(modelFile);/*from  ww w. ja v a  2  s.com*/

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

    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(MalletLdaTopicModelInferencer.class,
            MalletLdaTopicModelInferencer.PARAM_MODEL_LOCATION, modelFile);

    AnalysisEngineDescription writer = createEngineDescription(MalletLdaTopicsProportionsSortedWriter.class,
            MalletLdaTopicsProportionsSortedWriter.PARAM_TARGET_LOCATION, targetFile,
            MalletLdaTopicsProportionsSortedWriter.PARAM_N_TOPICS, nTopicsOutput);

    SimplePipeline.runPipeline(reader, segmenter, inferencer, writer);
    List<String> lines = FileUtils.readLines(targetFile);
    assertTrue(lines.get(0).matches(expectedLine0Regex));
    assertTrue(lines.get(1).matches(expectedLine1Regex));
    assertEquals(expectedLines, lines.size());

    /* assert first field */
    lines.stream().map(line -> line.split("\t")).forEach(fields -> assertTrue(fields[0].startsWith("dummy")));
}

From source file:com.denimgroup.threadfix.importer.cli.ScriptRunner.java

public long checkRunningAndFixStatements(String errorLogFile, String fixedSqlFile) {

    long errorCount = 0;

    File outputFixedScript = new File(fixedSqlFile);

    FileOutputStream fos = null;//ww  w. j ava  2s .c  o m
    try {
        List<String> lines = FileUtils.readLines(new File(errorLogFile));

        if (lines != null && lines.size() > 1) {
            fos = new FileOutputStream(outputFixedScript);
            OutputStreamWriter osw = new OutputStreamWriter(fos);

            String preLine = null;
            Map<String, Object> map = new HashMap<>();
            osw.write("SET FOREIGN_KEY_CHECKS=0;\n");
            for (String currentLine : lines) {

                if (!currentLine.contains("Error executing: INSERT INTO")) {
                    // Remove all weird characters if they cause 'incorrect string value' SQLExeption
                    if (currentLine.toLowerCase().contains("incorrect string value")) {
                        if (preLine != null) {
                            String fixedStatement = preLine.replace("Error executing: ", "")
                                    .replaceAll("[^\\x00-\\x7F]", "");
                            osw.write(fixedStatement + ";\n");
                        }
                    }
                    // If there is unknown column, then delete that column and its value
                    else if (currentLine.contains("MySQLSyntaxErrorException: Unknown column")) {
                        osw.write(getFixedUnknownColStatement(currentLine,
                                preLine.replace("Error executing: ", "")));
                    }
                    // too long string for column, cut it off
                    else if (currentLine.contains("Data too long for column")) {
                        map = exactTableFromString(preLine);
                        if (map == null || map.isEmpty()) {
                            LOGGER.error("Unable to fix statement: " + preLine);
                            LOGGER.error("Error was: " + currentLine);
                            osw.write(preLine.replace("Error executing: ", "") + ";\n");
                            continue;
                        }
                        osw.write(fixLongColumnStatement(preLine, currentLine, map));
                    }
                    // Older version of MySQL doesn't take time with fraction like '2015-10-28 13:01:59.289000000'
                    else if (currentLine.contains("Data truncation: Incorrect datetime value")) {
                        osw.write(fixDateWrongStatement(preLine, currentLine));
                    } else if (currentLine.contains("Packet for query is too large")) {
                        LOGGER.error(currentLine);
                        if (preLine.contains("INSERT INTO DOCUMENT")) {
                            LOGGER.warn(
                                    "You have failed to import a big document. Please upload that document by ThreadFix UI.");
                            map = exactTableFromString(preLine);
                            if (map != null && !map.isEmpty()) {
                                Map<String, String> fieldMap = (Map) map.get("tableFields");
                                for (String key : fieldMap.keySet()) {
                                    if (!key.equalsIgnoreCase("file")) {
                                        LOGGER.warn(key + ": " + fieldMap.get(key));
                                    }
                                }
                            }
                        } else {
                            osw.write(preLine.replace("Error executing: ", "") + ";\n");
                        }
                    } else if (currentLine
                            .contains("MySQLSyntaxErrorException: You have an error in your SQL syntax; "
                                    + "check the manual that corresponds to your MySQL server version")) {
                        osw.write(fixSyntaxStatement(preLine.replace("Error executing: ", ""), currentLine));
                    }
                    // Unresolved-yet SQLException, then write whole statement to fixed Sql script
                    else {
                        if (preLine != null && preLine.contains("Error executing: INSERT INTO")) {
                            osw.write(preLine.replace("Error executing: ", "") + ";\n");
                        }
                    }
                } else {
                    errorCount += 1;
                }
                preLine = currentLine;
            }

            osw.write("SET FOREIGN_KEY_CHECKS=1;\n");
            osw.close();
        }
    } catch (IOException e) {
        LOGGER.error("Error", e);
    }
    return errorCount;
}

From source file:edu.cmu.cs.lti.discoursedb.io.wikipedia.talk.converter.WikipediaTalkPageConverter.java

@Override
public void run(String... args) throws Exception {
    if (args.length != 8) {
        throw new RuntimeException("Incorrect number of launch parameters.");
    }//from   w  w w  .j a v a 2s.  c o m
    final String discourseName = args[0];

    final String dataSetName = args[1];
    if (dataSourceService.dataSourceExists(dataSetName)) {
        logger.warn("Dataset " + dataSetName
                + " has already been imported into DiscourseDB. Existing pages will be skipped.");
    }

    final String titleListFilename = args[2];
    File titleListFile = new File(titleListFilename);
    if (!titleListFile.exists() || !titleListFile.isFile()) {
        logger.error("Title list file " + titleListFilename + " cannot be read. Aborting ... ");
        return;
    }
    List<String> titles = FileUtils.readLines(titleListFile);

    logger.trace("Establishing connection to Wikipedia db...");
    DatabaseConfiguration dbconf = new DatabaseConfiguration();
    dbconf.setHost(args[3]);
    dbconf.setDatabase(args[4]);
    dbconf.setUser(args[5]);
    dbconf.setPassword(args[6]);
    dbconf.setLanguage(Language.valueOf(args[7]));
    Wikipedia wiki = new Wikipedia(dbconf);
    RevisionApi revApi = new RevisionApi(dbconf);

    RevisionBasedTalkPageExtractor extractor = null;
    logger.info("Start mapping Talk pages for " + titles.size() + " articles to DiscourseDB...");
    int tpNum = 1;
    for (String title : titles) {
        //first check if we alrady have the discussions from this article from a previous import
        if (discoursePartService.exists(discourseService.createOrGetDiscourse(discourseName), title,
                DiscoursePartTypes.TALK_PAGE)) {
            logger.warn("Discussions for article " + title + "have already been imported. Skipping ...");
            continue;
        }

        logger.info("Segmenting Talk Pages for article " + title);
        extractor = new RevisionBasedTalkPageExtractor(wiki, revApi, title, false, true);
        List<TalkPage> talkPages = extractor.getTalkPages();
        for (TalkPage tp : talkPages) {
            if (tp != null) {
                logger.info("Mapping Talk Page #" + tpNum++);
                converterService.mapTalkPage(discourseName, dataSetName, title, tp);
            }
        }
    }
    logger.info("Finished mapping Talk pages.");

    //manually close the hibernate session for the Wikipedia connection which is not managed by Spring
    WikiHibernateUtil.getSessionFactory(dbconf).close();
}

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

/**
 * Test, if concept labels are created properly. The input file contains a
 * URI in each line.// ww  w  .  j  a va  2s.com
 * 
 * @param inFile
 *            input file
 * @throws IOException
 */
@SuppressWarnings("unchecked")
@Test(dataProviderClass = TestFileProvider.class, dataProvider = "labelTestFiles", groups = { "functest" })
public void getLabel(File inFile) throws IOException {

    // TODO. With the recent change to the OntologyUtil class, labels are
    // not extracted from a concept's uri any more. Instead, the RDF Label
    // annotations are used. Thus, a concept can have more than one label now.
    // The problem is that no assumption about the order in which the labels
    // are returned can be made. We have to rewrite the test case such that
    // it does not make this assumption.

    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> uris = FileUtils.readLines(inFile);
    PrintWriter w = new PrintWriter(new FileWriter(outFile));

    for (String u : uris) {
        OntClass clazz = index.getModel().getOntClass(u);
        List<String> labels = OntologyUtils.getLabels(clazz);
        for (String l : labels) {
            log.debug("** " + u + " => " + l);
            w.println(l);
        }
    }
    w.flush();
    w.close();

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

From source file:io.fabric8.maven.docker.util.DockerFileUtilTest.java

@Test
public void interpolate() throws Exception {
    MojoParameters params = mockMojoParams();
    Map<String, String> filterMapping = new HashMap<>();
    filterMapping.put("none", "false");
    filterMapping.put("var", "${*}");
    filterMapping.put("at", "@");

    for (Map.Entry<String, String> entry : filterMapping.entrySet()) {
        for (int i = 1; i < 2; i++) {
            File dockerFile = getDockerfilePath(i, entry.getKey());
            File expectedDockerFile = new File(dockerFile.getParent(), dockerFile.getName() + ".expected");
            File actualDockerFile = createTmpFile(dockerFile.getName());
            FixedStringSearchInterpolator interpolator = DockerFileUtil.createInterpolator(params,
                    entry.getValue());//  w w  w  .  ja  v a  2  s.  com
            FileUtils.write(actualDockerFile, DockerFileUtil.interpolate(dockerFile, interpolator), "UTF-8");
            // Compare text lines without regard to EOL delimiters
            assertEquals(FileUtils.readLines(expectedDockerFile), FileUtils.readLines(actualDockerFile));
        }
    }
}

From source file:com.mythesis.profileanalysis.WordVectorFinder.java

/**
 * a method that cleans the word vector of a certain sub-domain
 * @param directory the directory where im gonna save the results
 * @param profile the profile i want to get the wordvector 
 * @param master the general domain/*  w w  w.  ja v a  2 s .  c  o  m*/
 */
public void cleanWordVector(String directory, String profile, String master) {

    List<String> masterVector = new ArrayList<>();
    List<String> wordVector = new ArrayList<>();
    File wordsFile;
    try {
        wordsFile = new File(directory + "\\wordVector" + master + ".txt");
        masterVector = FileUtils.readLines(wordsFile); // get the word vector of the general domain
        wordsFile = new File(directory + "\\wordVector" + profile + ".txt");
        wordVector = FileUtils.readLines(wordsFile); // get the word vector of the sub-domain
    } catch (IOException ex) {
        Logger.getLogger(WordVectorFinder.class.getName()).log(Level.SEVERE, null, ex);
    }

    for (String s : masterVector) {
        if (wordVector.contains(s))
            wordVector.remove(s); // remove any word that occurs in general domain's word vector
    }

    // save the new word vector
    File wordVectorPath = new File(directory + "\\wordVectorCleaned" + profile + ".txt");
    try {
        FileUtils.writeLines(wordVectorPath, wordVector);
    } catch (IOException ex) {
        Logger.getLogger(WordVectorFinder.class.getName()).log(Level.SEVERE, null, ex);
    }
}