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:edu.hawaii.soest.pacioos.text.SocketTextSourceTest.java

/**
 * Test multiple instrument file formats and instrument configurations:
 * Data from NS02//from  w  ww.ja va 2 s  .  c o m
 * Data from NS03
 * Test method for {@link edu.hawaii.soest.pacioos.text.SocketTextSource#SocketTextSource(org.apache.commons.configuration.XMLConfiguration)}.
 */
@Test
public void testSocketTextSource() {

    testMockInstruments = new ArrayList<String>();
    testMockInstruments.add("AW02XX_001CTDXXXXR00");
    testMockInstruments.add("WK01XX_001CTDXXXXR00");
    testMockInstruments.add("KN0101_010TCHNXXXR00");
    testMockInstruments.add("MU01XX_001YSIXXXXR00");

    // test each mock instrument file, using file ending naming conventions
    for (String instrument : testMockInstruments) {
        // start up the mock data source
        mockCTDData = testResourcesDirectory + "edu/hawaii/soest/pacioos/text/" + instrument + "-mock-data.txt";
        mockDataSource = new MockDataSource(mockCTDData);
        mockDataSourceThread = new Thread(mockDataSource);
        mockDataSourceThread.start();

        String configLocation = testResourcesDirectory + "edu/hawaii/soest/pacioos/text/" + instrument
                + "-instrument-config.xml";

        SimpleTextSource socketTextSource = null;
        try {

            // start the SocketTextSource
            socketTextSource = TextSourceFactory.getSimpleTextSource(configLocation);
            socketTextSource.start();

            // wait while data are streamed from the mock data source
            while (mockDataSourceThread.isAlive()) {
                Thread.sleep(1000);

            }

            // stop the SocketTextSource
            socketTextSource.stop();

            // count the number of samples intended to be sent to the DataTurbine
            File ctdData = new File(mockCTDData);
            int numberOfIntendedSamples = 0;
            List<String> lines = new ArrayList<String>();
            try {
                lines = FileUtils.readLines(ctdData);
                for (String line : lines) {
                    if (line.matches(socketTextSource.getPattern())) {
                        numberOfIntendedSamples++;
                    }
                }
                //numberOfIntendedSamples = lines.size();
            } catch (IOException e) {
                fail("Couldn't read data file: " + e.getMessage());

            }

            // retrieve the data from the DataTurbine
            ChannelMap requestMap = new ChannelMap();
            int entryIndex = requestMap
                    .Add(socketTextSource.getRBNBClientName() + "/" + socketTextSource.getChannelName());
            log.debug("Request Map: " + requestMap.toString());
            Sink sink = new Sink();
            sink.OpenRBNBConnection(socketTextSource.getServer(), "lastEntrySink");
            sink.Request(requestMap, 0., 60000., "newest");
            ChannelMap responseMap = sink.Fetch(60000); // get data within 60 seconds
            String[] dtLines = responseMap.GetDataAsString(entryIndex);
            int numberOfSuccessfulSamples = dtLines.length;

            log.info("Intended samples  : " + numberOfIntendedSamples);
            log.info("Successful samples: " + numberOfSuccessfulSamples);

            assertEquals(numberOfIntendedSamples, numberOfSuccessfulSamples);

            // shut down the data source
            if (mockDataSource != null) {
                mockDataSource.stop();

            }

        } catch (ConfigurationException e) {
            log.error(e.getMessage());
            e.printStackTrace();
            fail("Couldn't configure a driver using " + configLocation);

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

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

From source file:jenkins.model.RunIdMigrator.java

/**
 * @return whether there was a file to load
 *//*from w w  w  .j  a v a  2s.c  o m*/
private boolean load(File dir) {
    File f = new File(dir, MAP_FILE);
    if (!f.isFile()) {
        return false;
    }
    if (f.length() == 0) {
        return true;
    }
    idToNumber = new TreeMap<String, Integer>();
    try {
        for (String line : FileUtils.readLines(f)) {
            int i = line.indexOf(' ');
            idToNumber.put(line.substring(0, i), Integer.parseInt(line.substring(i + 1)));
        }
    } catch (Exception x) { // IOException, IndexOutOfBoundsException, NumberFormatException
        LOGGER.log(WARNING, "could not read from " + f, x);
    }
    return true;
}

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.//w  w  w  .j  a  v a  2s . c  o m
 * 
 * @param inFile
 *            a list of terms
 * @throws IOException
 */
@SuppressWarnings("unchecked")
@Test(dataProviderClass = TestFileProvider.class, dataProvider = "synonymTestFiles", groups = { "functest" })
public void synonyms(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.getSynonyms(clazz));
    }
    w.flush();
    w.close();

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

From source file:jenkins.plugins.asqatasun.AsqatasunRunner.java

/**
 * //from w  w w  . j a  va  2 s.c  om
 * @param logFile
 * @param ps
 * @throws IOException 
 */
public void extractDataAndPrintOut(File logFile, PrintStream ps) throws IOException {
    ps.println("");
    boolean isFirstMark = true;
    boolean isFirstNbPassed = true;
    boolean isFirstNbFailed = true;
    boolean isFirstNbFailedOccurences = true;
    boolean isFirstNbNmi = true;
    boolean isFirstNbNa = true;
    boolean isFirstNbNt = true;
    for (Object obj : FileUtils.readLines(logFile)) {
        String line = (String) obj;
        if (StringUtils.startsWith(line, "Subject")) {
            ps.println("");
            ps.println(line);
        } else if (StringUtils.startsWith(line, "Audit terminated")) {
            ps.println(line);
        } else if (StringUtils.startsWith(line, "RawMark")) {
            ps.println(line.replace("RawMark", "Mark"));
            if (isFirstMark) {
                mark = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).replaceAll("%", "")
                        .trim();
                isFirstMark = false;
            }
        } else if (StringUtils.startsWith(line, "Nb Passed")) {
            nbPassed = getNbStatus(ps, isFirstNbPassed, line);
            isFirstNbPassed = false;
        } else if (StringUtils.startsWith(line, "Nb Failed test")) {
            nbFailed = getNbStatus(ps, isFirstNbFailed, line);
            isFirstNbFailed = false;
        } else if (StringUtils.startsWith(line, "Nb Failed occurences")) {
            nbFailedOccurences = getNbStatus(ps, isFirstNbFailedOccurences, line);
            isFirstNbFailedOccurences = false;
        } else if (StringUtils.startsWith(line, "Nb Pre-qualified")) {
            nbNmi = getNbStatus(ps, isFirstNbNmi, line);
            isFirstNbNmi = false;
        } else if (StringUtils.startsWith(line, "Nb Not Applicable")) {
            nbNa = getNbStatus(ps, isFirstNbNa, line);
            isFirstNbNa = false;
        } else if (StringUtils.startsWith(line, "Nb Not Tested")) {
            nbNt = getNbStatus(ps, isFirstNbNt, line);
            isFirstNbNt = false;
        } else if (StringUtils.startsWith(line, "Audit Id")) {
            ps.println(line);
            auditId = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).trim();
        }
    }
    ps.println("");
}

From source file:de.tudarmstadt.ukp.dkpro.tc.ml.uima.TcAnnotatorDocument.java

private List<Object> initializeParameters() throws Exception {
    List<Object> parameters = new ArrayList<>();
    List<String> modelParameters = FileUtils.readLines(new File(tcModelLocation, MODEL_PARAMETERS));
    for (String parameter : modelParameters) {
        if (!parameter.startsWith("#")) {
            String[] parts = parameter.split("=");
            parameters.add(parts[0]);/*www.j ava  2  s.c  om*/
            parameters.add(parts[1]);
        }
    }

    return parameters;
}

From source file:com.adguard.compiler.FilterUtils.java

/**
 * Gets javascript injection rules./*w ww.ja  va  2 s  .  c om*/
 *
 * @param source Extension source
 * @return Map filterId-List of rules
 * @throws IOException
 */
public static Map<Integer, List<String>> getScriptRules(File source) throws IOException {

    Map<Integer, List<String>> filterScriptRules = new HashMap<Integer, List<String>>();

    File filtersDir = new File(source, "filters");
    for (int filterId = 1; filterId <= 10; filterId++) {
        File filterFile = new File(filtersDir, "filter_" + filterId + ".txt");
        List<String> lines = FileUtils.readLines(filterFile);
        List<String> scriptRules = new ArrayList<String>();
        for (String line : lines) {
            if (line.contains("#%#")) {
                scriptRules.add(line.trim());
            }
        }

        if (scriptRules.size() > 0) {
            filterScriptRules.put(filterId, scriptRules);
        }
    }

    return filterScriptRules;
}

From source file:com.example.mydtapp.JdbcInputAppTest.java

@Test
public void testApplication() throws Exception {
    try {/*w w  w. j a  va  2s. co m*/
        LocalMode lma = LocalMode.newInstance();
        Configuration conf = new Configuration(false);
        conf.addResource(this.getClass().getResourceAsStream("/META-INF/properties-SimpleJdbcToHDFSApp.xml"));
        lma.prepareDAG(new JdbcHDFSApp(), conf);
        LocalMode.Controller lc = lma.getController();
        lc.runAsync();

        // wait for output files to roll      
        Thread.sleep(5000);

        String[] extensions = { "dat.0", "tmp" };
        Collection<File> list = FileUtils.listFiles(new File(FILE_NAME), extensions, false);
        Assert.assertEquals("Records in file", 10, FileUtils.readLines(list.iterator().next()).size());

    } catch (ConstraintViolationException e) {
        Assert.fail("constraint violations: " + e.getConstraintViolations());
    }
}

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

public CosineSimilarity(WeightingModeTf modeTf, WeightingModeIdf modeIdf, NormalizationMode normMode,
        String idfScoresFile) {/*w  ww.  java  2s  .  c  o m*/
    HashMap<String, Double> idfValues;
    if (idfScoresFile != null) {
        idfValues = new HashMap<String, Double>();
        try {
            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(modeTf, modeIdf, normMode, idfValues);
}

From source file:com.bitplan.pdfindex.TestPdfindexer.java

@Test
/**//from w  ww  . j  a  v a 2 s  .  c  o m
 * test using an URI
 */
public void testURI() throws IOException {
    String docURI = "http://eprints.nottingham.ac.uk/249/1/cajun.pdf";
    String htmlOutputFileName = "test/cajun.html";
    this.testPdfIndexer(docURI, "test/indices/cajun", "Adobe,IBM,MS-DOS,Adobe Illustrator", htmlOutputFileName);
    List<String> lines = FileUtils.readLines(new File(htmlOutputFileName));
    /**
     * 24:        <li>Adobe Illustrator:2
    * 25:          <a href='http://eprints.nottingham.ac.uk/249/1/cajun.pdf' title='http://eprints.nottingham.ac.uk/249/1/cajun.pdf'>http://eprints.nottingham.ac.uk/249/1/cajun.pdf</a><br/>
    * 26:          <a href='http://eprints.nottingham.ac.uk/249/1/cajun.pdf#page=4' title='http://eprints.nottingham.ac.uk/249/1/cajun.pdf#page=4'>4</a>
    * 27:          <a href='http://eprints.nottingham.ac.uk/249/1/cajun.pdf#page=5' title='http://eprints.nottingham.ac.uk/249/1/cajun.pdf#page=5'>5</a>
    * 28:        </li>   
     */
    checkLines(lines, 41, 27, "cajun.pdf#page=5");
}

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

private static List<List<String>> readFileToLists(String fileLocationString) throws IOException {

    File fileLocation = new File(fileLocationString);
    List<List<String>> returnList = new ArrayList<List<String>>();
    for (String line : FileUtils.readLines(fileLocation)) {
        line = line.replace("\n", "");
        List<String> lineList = new ArrayList<String>();
        for (String word : line.split("\t")) {
            lineList.add(word);//from   www. jav  a  2  s  .  com
        }
        if (lineList.size() > 1) {
            returnList.add(lineList);
        }
    }

    return returnList;
}