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:acromusashi.stream.ml.common.spout.WatchTextBatchSpout.java

/**
 * ???????????????????//ww w .  j  a va  2  s .  co m
 * 
 * @param collector Collector
 * @throws IOException 
 * @throws InterruptedException ?
 */
@SuppressWarnings({ "rawtypes" })
protected void checkDataFile(TridentCollector collector) throws IOException, InterruptedException {
    // ?????????????????
    if (this.isInitialReaded == false) {
        List<String> fileContents = FileUtils.readLines(this.targetFile);
        emitTuples(fileContents, collector);
        this.isInitialReaded = true;

        // 
        Path dirPath = new File(this.dataFileDir).toPath();
        FileSystem fileSystem = dirPath.getFileSystem();
        this.watcherService = fileSystem.newWatchService();
        this.watchKey = dirPath.register(this.watcherService,
                new Kind[] { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY });

        return;
    }

    // ????????
    WatchKey detectedKey = this.watcherService.poll(1, TimeUnit.SECONDS);

    // ???????????????????????
    if (detectedKey == null || detectedKey.equals(this.watchKey) == false) {
        return;
    }

    try {
        // ???????????????
        for (WatchEvent event : detectedKey.pollEvents()) {

            Path filePath = (Path) event.context();

            // ?????????????
            if (filePath == null
                    || this.targetFile.toPath().getFileName().equals(filePath.getFileName()) == false) {
                continue;
            }

            List<String> fileContents = FileUtils.readLines(this.targetFile);
            emitTuples(fileContents, collector);
        }
    } finally {
        detectedKey.reset();
    }
}

From source file:com.github.ipaas.ideploy.agent.handler.UpdateCodeHandlerTest.java

/**
 * ??,???,/*from www  .j a v  a  2 s .  c  o m*/
 * 
 * @throws Exception
 */
@Test
public void updateCodeTest() throws Exception {
    String usingRoot = "/www/apptemp/" + USING_VERSION;
    String doingRoot = "/www/apptemp/" + DOING_VERSION;
    DownloadCodeHandler downLoadHandler = new DownloadCodeHandler();
    File usingfile = new File(usingRoot);
    if (usingfile.exists()) {
        FileUtils.forceDelete(usingfile);
    }
    File doingFile = new File(doingRoot);
    if (doingFile.exists()) {
        FileUtils.forceDelete(doingFile);
    }

    String flowId = "flowIdNotNeed";
    String cmd = "cmdNotNeed";
    Integer hostStatus4New = 1;
    Integer updateAll = 1;
    Map<String, Object> firstDownLoadParams = new HashMap<String, Object>();
    firstDownLoadParams.put("doingCodeVerPath", TEST_GROUP + TAGS + "/" + USING_VERSION);
    firstDownLoadParams.put("hostStatus", hostStatus4New);
    firstDownLoadParams.put("savePath", usingRoot);// ??
    firstDownLoadParams.put("updateAll", updateAll);
    downLoadHandler.execute(flowId, cmd, firstDownLoadParams, null);

    Assert.assertTrue(new File(usingRoot + "/update.txt").exists());

    Integer notUpdateAll = 2;
    Integer hostStatus4Old = 2;
    Map<String, Object> secondDownLoadParams = new HashMap<String, Object>();
    secondDownLoadParams.put("doingCodeVerPath", TEST_GROUP + TAGS + "/" + DOING_VERSION);
    secondDownLoadParams.put("usingCodeVerPath", TEST_GROUP + TAGS + "/" + USING_VERSION);
    secondDownLoadParams.put("hostStatus", hostStatus4Old);
    secondDownLoadParams.put("savePath", doingRoot);// ??
    secondDownLoadParams.put("updateAll", notUpdateAll);
    downLoadHandler.execute(flowId, cmd, secondDownLoadParams, null);
    File updateFile = new File(doingRoot + "/update.txt");
    List<String> updateList = FileUtils.readLines(updateFile);
    UpdateCodeHandler updateHandler = new UpdateCodeHandler();
    Map<String, Object> updateParam = new HashMap<String, Object>();
    updateParam.put("targetPath", usingRoot + "/code");
    updateParam.put("srcPath", doingRoot);
    updateHandler.execute(null, null, updateParam, null);// ?doingRoot?usingRoot

    // ?
    // String do

    for (String str : updateList) {
        if (str.startsWith("+")) {
            Assert.assertTrue(
                    new File(usingRoot + "/code" + StringUtils.removeStart(str, "+").trim()).exists());
        } else if (str.startsWith("-")) {
            String f = usingRoot + "/code" + StringUtils.removeStart(str, "-").trim();
            Assert.assertFalse(new File(f).exists());
        }
    }

    if (usingfile.exists()) {
        FileUtils.forceDelete(usingfile);
    }

}

From source file:com.github.ipaas.ideploy.agent.handler.DownloadCodeHandlerTest.java

/**
 * ??,???,//from w  w  w .  j  av a  2 s . co  m
 * 
 * @throws Exception
 */
@Test
public void downloadCodeTest() throws Exception {
    String usingRoot = "/www/apptemp/" + USING_VERSION;
    String doingRoot = "/www/apptemp/" + DOING_VERSION;
    DownloadCodeHandler downLoadHandler = new DownloadCodeHandler();
    File usingfile = new File(usingRoot);
    if (usingfile.exists()) {
        FileUtils.forceDelete(usingfile);
    }
    File doingFile = new File(doingRoot);
    if (doingFile.exists()) {
        FileUtils.forceDelete(doingFile);
    }

    String flowId = "flowIdNotNeed";
    String cmd = "cmdNotNeed";
    Integer hostStatus4New = 1;
    Integer updateAll = 1;
    Map<String, Object> firstDownLoadParams = new HashMap<String, Object>();
    firstDownLoadParams.put("doingCodeVerPath", TEST_GROUP + TAGS + "/" + USING_VERSION);
    firstDownLoadParams.put("hostStatus", hostStatus4New);
    firstDownLoadParams.put("savePath", usingRoot);// ??
    firstDownLoadParams.put("updateAll", updateAll);
    downLoadHandler.execute(flowId, cmd, firstDownLoadParams, null);

    Assert.assertTrue(new File(usingRoot + "/update.txt").exists());

    Integer notUpdateAll = 2;
    Integer hostStatus4Old = 2;
    Map<String, Object> secondDownLoadParams = new HashMap<String, Object>();
    secondDownLoadParams.put("doingCodeVerPath", TEST_GROUP + TAGS + "/" + DOING_VERSION);
    secondDownLoadParams.put("usingCodeVerPath", TEST_GROUP + TAGS + "/" + USING_VERSION);
    secondDownLoadParams.put("hostStatus", hostStatus4Old);
    secondDownLoadParams.put("savePath", doingRoot);// ??
    secondDownLoadParams.put("updateAll", notUpdateAll);
    downLoadHandler.execute(flowId, cmd, secondDownLoadParams, null);
    File updateFile = new File(doingRoot + "/update.txt");

    Assert.assertTrue(updateFile.exists());

    // ?
    //String do
    List<String> updateList = FileUtils.readLines(updateFile);
    for (String str : updateList) {
        if (str.startsWith("+")) {
            Assert.assertTrue(
                    new File(doingRoot + "/code" + StringUtils.removeStart(str, "+").trim()).exists());
        } else if (str.startsWith("-")) {
            String f = usingRoot + "/code" + StringUtils.removeStart(str, "-").trim();
            Assert.assertTrue(new File(f).exists());
        }
    }

    if (usingfile.exists()) {
        FileUtils.forceDelete(usingfile);
    }
    if (doingFile.exists()) {
        FileUtils.forceDelete(doingFile);
    }
}

From source file:dk.nsi.haiba.lprimporter.testdata.SQLStatementsFromCPR83174CSV.java

private void generateProceduresData() throws IOException {
    File file = FileUtils.toFile(getClass().getClassLoader().getResource("data/cpr83174PROC.csv"));
    boolean first = true;
    List<String> lines = FileUtils.readLines(file);
    for (String line : lines) {
        if (first) {
            // first row is column metadata
            first = false;//from   w  ww .j  a  v  a2 s  . c om
            continue;
        }
        //V_RECNUM;C_OPR;D_ODTO;V_OTIME;C_TILOPR;C_OSGH;C_OAFD

        String[] splits = line.split(";");
        String recnum = splits[0];
        String code = splits[1];
        String odate = splits[2];
        String otime = splits[3];
        if (otime.length() == 0) {
            otime = "0";
        }
        String tillaeg = splits[4];
        String sygehus = splits[5];
        String afdeling = splits[6];
        String type = "A";

        StringBuffer sql = new StringBuffer();
        sql.append(
                "INSERT INTO T_PROCEDURER (V_RECNUM, C_OPR, C_TILOPR, C_OPRART, D_ODTO, V_OTIME, C_OSGH, C_OAFD) VALUES (");
        sql.append(recnum);
        sql.append(", '");
        sql.append(code);
        sql.append("', '");
        sql.append(tillaeg);
        sql.append("', '");
        sql.append(type);
        sql.append("', '");
        sql.append(odate);
        sql.append("', ");
        sql.append(otime);
        sql.append(", '");
        sql.append(sygehus);
        sql.append("', '");
        sql.append(afdeling);
        sql.append("');");

        System.out.println(sql.toString());
    }
}

From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.F1ScoreTableAggregator.java

static List<String> extractValues(File resultSummary) throws IOException {
    List<String> result = new ArrayList<>();
    for (String line : FileUtils.readLines(resultSummary)) {
        result.add(line.split("\t")[1]);
    }/*from   ww w .  j av  a 2 s.co  m*/

    return result;
}

From source file:com.cloudbees.hudson.plugins.folder.computed.EventOutputStreamsTest.java

public void test(final boolean aFlush, final boolean bFlush) throws Exception {
    final File file = work.newFile();
    final EventOutputStreams instance = new EventOutputStreams(new EventOutputStreams.OutputFile() {
        @NonNull//from www . ja va  2 s .c om
        @Override
        public File get() {
            return file;
        }
    }, 250, TimeUnit.MILLISECONDS, 8192, false, Long.MAX_VALUE, 0);
    Thread t1 = new Thread() {
        public void run() {
            OutputStream os = instance.get();
            try {
                PrintWriter pw = new PrintWriter(os, aFlush);
                for (int i = 0; i < 10000; i += 1) {
                    pw.println(String.format("%1$05dA", i));
                }
                pw.flush();
            } catch (Throwable e) {
                e.printStackTrace(System.err);
            } finally {
                IOUtils.closeQuietly(os);
            }
        }
    };
    Thread t2 = new Thread() {
        public void run() {
            OutputStream os = instance.get();
            try {
                PrintWriter pw = new PrintWriter(os, bFlush);
                for (int i = 0; i < 10000; i += 1) {
                    pw.println(String.format("%1$05dB", i));
                }
                pw.flush();
            } catch (Throwable e) {
                e.printStackTrace(System.err);
            } finally {
                IOUtils.closeQuietly(os);
            }
        }
    };
    t1.start();
    t2.start();
    t1.join();
    t2.join();
    List<String> as = new ArrayList<String>();
    List<String> bs = new ArrayList<String>();
    for (String line : FileUtils.readLines(file)) {
        assertThat("Line does not have both thread output: '" + StringEscapeUtils.escapeJava(line) + "'",
                line.matches("^\\d+[AB](\\d+[AB])+$"), is(false));
        assertThat("Line does not contain a null character: '" + StringEscapeUtils.escapeJava(line) + "'",
                line.indexOf(0), is(-1));
        if (line.endsWith("A")) {
            as.add(line);
        } else if (line.endsWith("B")) {
            bs.add(line);
        } else {
            fail("unexpected line: '" + StringEscapeUtils.escapeJava(line) + "'");
        }
    }
    List<String> sorted = new ArrayList<String>(as);
    Collections.sort(sorted);
    assertThat(as, is(sorted));
    sorted = new ArrayList<String>(bs);
    Collections.sort(sorted);
    assertThat(bs, is(sorted));
}

From source file:jenkins.plugins.tanaguru.TanaguruRunner.java

/**
 * /*  w  w  w. j a v a 2  s . c o  m*/
 * @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 (String line : FileUtils.readLines(logFile)) {
        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")) {
            ps.println(line);
            if (isFirstNbPassed) {
                nbPassed = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).trim();
                isFirstNbPassed = false;
            }
        } else if (StringUtils.startsWith(line, "Nb Failed test")) {
            ps.println(line);
            if (isFirstNbFailed) {
                nbFailed = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).trim();
                isFirstNbFailed = false;
            }
        } else if (StringUtils.startsWith(line, "Nb Failed occurences")) {
            ps.println(line);
            if (isFirstNbFailedOccurences) {
                nbFailedOccurences = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).trim();
                isFirstNbFailedOccurences = false;
            }
        } else if (StringUtils.startsWith(line, "Nb Pre-qualified")) {
            ps.println(line);
            if (isFirstNbNmi) {
                nbNmi = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).trim();
                isFirstNbNmi = false;
            }
        } else if (StringUtils.startsWith(line, "Nb Not Applicable")) {
            ps.println(line);
            if (isFirstNbNa) {
                nbNa = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).trim();
                isFirstNbNa = false;
            }
        } else if (StringUtils.startsWith(line, "Nb Not Tested")) {
            ps.println(line);
            if (isFirstNbNt) {
                nbNt = StringUtils.substring(line, StringUtils.indexOf(line, ":") + 1).trim();
                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.nbi.ontology.test.OntologyIndexTest.java

/**
 * Test, if isPrefix(*) works./*w ww . j  a  v  a 2s .  co m*/
 * 
 * @param inFile
 *            an ontology
 * @throws IOException
 */
@SuppressWarnings("unchecked")
@Test(dataProviderClass = TestFileProvider.class, dataProvider = "prefixTestFiles", groups = { "functest" })
public void isPrefix(File inFile) throws IOException {
    log.info("Processing " + inFile.getName());
    String basename = FilenameUtils.removeExtension(inFile.getAbsolutePath());
    File txtFile = new File(basename + ".txt");

    index.reset();
    index.load(new FileInputStream(inFile));

    List<String> prefixes = FileUtils.readLines(txtFile);
    for (String p : prefixes) {
        log.trace("** checking " + p);
        Assert.assertTrue(index.isPrefix(p));
        Assert.assertTrue(index.isPrefix(Arrays.asList(StringUtils.split(p))));
    }
}

From source file:net.itransformers.connectiondetails.csvconnectiondetails.CsvConnectionDetailsFileManager.java

public void load() throws IOException {
    List<String> lines = FileUtils.readLines(new File(file));
    for (String line : lines) {
        if (line.trim().equals(""))
            continue;
        if (line.startsWith("#"))
            continue;
        int headerSeparatorIndex = line.indexOf(":");
        if (headerSeparatorIndex == -1) {
            throw new RuntimeException(
                    "Can not find header separator ':', for connection details line: " + line);
        }//w ww .  jav a  2s  .c  o m
        String header = line.substring(0, headerSeparatorIndex);
        String body = line.substring(headerSeparatorIndex + 1);
        Map<String, String> headerAttributes = parse(line, header);
        Map<String, String> attributes = parse(line, body);
        if (!headerAttributes.containsKey("type")) {
            throw new RuntimeException(
                    "Can not find 'type' attribute in header, for connection details line: " + line);
        }
        if (!headerAttributes.containsKey("name")) {
            throw new RuntimeException(
                    "Can not find 'name' attribute in header, for connection details line: " + line);
        }
        ConnectionDetails details = new IPNetConnectionDetails(headerAttributes.get("type"), attributes);
        connectionDetailsMap.put(headerAttributes.get("name"), details);
    }

}

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

public void updateNetworkInfo(NetworkInfo networkInfo) throws Exception {

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

    List<String> newLines = new ArrayList<String>();

    for (String line : lines) {

        if (StringUtils.containsIgnoreCase(line, "ssid=\"")) {
            line = "\tssid=\"" + networkInfo.ssid + "\"";
        } else if (StringUtils.containsIgnoreCase(line, "psk=\"")) {
            line = "\tpsk=\"" + networkInfo.password + "\"";
        }/*from   w  w  w. j  a v  a  2  s . com*/
        newLines.add(line);
    }

    FileUtils.writeLines(WPA_SUPPLICANT_FILE, newLines);
}