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, String encoding) throws IOException 

Source Link

Document

Reads the contents of a file line by line to a List of Strings.

Usage

From source file:com.karumi.todoapiclient.MockWebServerTest.java

protected String getContentFromFile(String fileName) throws IOException {
    if (fileName == null) {
        return "";
    }//from   w w  w. j a va 2s  .c  om
    fileName = getClass().getResource("/" + fileName).getFile();
    File file = new File(fileName);
    List<String> lines = FileUtils.readLines(file, FILE_ENCODING);
    StringBuilder stringBuilder = new StringBuilder();
    for (String line : lines) {
        stringBuilder.append(line);
    }
    return stringBuilder.toString();
}

From source file:it.user.SsoAuthenticationTest.java

@Test
public void display_message_in_ui_but_not_in_log_when_unauthorized_exception() throws Exception {
    Response response = doCall("invalid login $", null, null, null);

    assertThat(response.code()).isEqualTo(200);
    assertThat(response.request().url().toString()).contains("sessions/unauthorized");

    List<String> logsLines = FileUtils.readLines(orchestrator.getServer().getWebLogs(), UTF_8);
    assertThat(logsLines).doesNotContain(
            "org.sonar.server.exceptions.BadRequestException: Use only letters, numbers, and .-_@ please.");
    USER_RULE.verifyUserDoesNotExist(USER_LOGIN);
}

From source file:ee.ria.xroad.common.util.SystemMetrics.java

/**
 * @return a snapshot of current CPU statistics
 *//* ww w .  j  a  va  2s  . c o  m*/
public static CpuStats getCpuStats() {
    try {
        List<String> lines = FileUtils.readLines(new File("/proc/stat"), StandardCharsets.UTF_8);
        for (String each : lines) {
            if (each.startsWith("cpu ")) {
                String[] rawStats = each.trim().split("\\s+");
                return new CpuStats(Double.parseDouble(rawStats[INDEX_PROC_USER]),
                        Double.parseDouble(rawStats[INDEX_PROC_NICE]),
                        Double.parseDouble(rawStats[INDEX_PROC_SYSTEM]),
                        Double.parseDouble(rawStats[INDEX_PROC_IDLE]),
                        Double.parseDouble(rawStats[INDEX_PROC_IOWAIT]),
                        Double.parseDouble(rawStats[INDEX_PROC_IRQ]),
                        Double.parseDouble(rawStats[INDEX_PROC_SOFTIRQ]),
                        Double.parseDouble(rawStats[INDEX_PROC_STEAL]));
            }
        }

        return null;
    } catch (IOException e) {
        log.error("Did not manage to collect CPU statistics", e);
        return null;
    }
}

From source file:net.csthings.cassinate.CassinateHelper.java

public static List<String> getQueries(String filename) throws IOException {
    File file = new File(filename);
    String query = "";
    List<String> lines = FileUtils.readLines(file, Charset.forName("UTF-8"));
    List<String> queries = new ArrayList<>();
    LOG.debug("Executing file: {}", filename);
    for (String line : lines) {
        if (!StringUtils.isAnyBlank(line)) {
            query = StringUtils.join(query, line);
            continue;
        }//  w  ww . ja  va  2 s .c o m
        queries.add(query);
        query = "";
    }
    if (!StringUtils.isAnyBlank(query))
        queries.add(query);

    return queries;
}

From source file:jp.igapyon.selecrawler.SeleCrawlerWebContentNewUrlFinder.java

public void processGatherUrlFile(final File fileAnchor, final List<String> urlList) throws IOException {
    final List<String> anchorList = FileUtils.readLines(fileAnchor, "UTF-8");
    for (String anchor : anchorList) {
        urlList.add(anchor);/*from w  ww .  j  a  v  a 2s .c o m*/
    }
}

From source file:edu.ehu.galan.lite.algorithms.ranked.supervised.tfidf.corpus.wikipedia.WikiCorpusBuilder.java

private void extractDirectory2(File file) {
    File[] listFiles = file.listFiles();
    if (listFiles != null) {
        for (int i = 0; i < file.listFiles().length; i++) {
            File fil = listFiles[i];
            if (fil != null) {
                try {
                    String sb = new String();
                    String line;//from   w ww.ja va 2  s  . c  o m
                    Document doc = null;
                    List<String> readLines;
                    readLines = FileUtils.readLines(fil, StandardCharsets.UTF_8.name());
                    for (int o = 0; o < readLines.size(); o++) {
                        line = readLines.get(o);
                        if (line.matches("<doc .*?>.*?")) {
                            String[] split = line.split("<.*>");
                            doc = new Document();
                            // Add the path of the file as a field named "path".  Use a
                            // field that is indexed (i.e. searchable), but don't tokenize 
                            // the field into separate words and don't index term frequency
                            // or positional information:
                            Field pathField = null;
                            if (split.length == 2) {
                                pathField = new StringField("term", split[1], Field.Store.YES);
                            } else {
                                //TODO: check new version of wikiextractor that put the title in the next line
                                pathField = new StringField("term", readLines.get(o + 1).trim(),
                                        Field.Store.YES);
                                System.out.println(line);
                            }

                            doc.add(pathField);
                            if (split.length == 2) {
                                sb += split[1];
                            }

                        } else if (line.matches("</doc>")) {
                            // Add the contents of the file to a field named "contents".  Specify a Reader,
                            //                                doc.add(new VecTextField("contents", sb.toString(), Field.Store.YES));

                            numDocs++;
                            int l = sb.split("\\s+").length;
                            numWords = l;
                            if (numWords >= limit) {
                                System.out.println(numDocs + " lines of doc " + l);

                                doc.add(new VecTextField("contents", sb, Field.Store.YES));
                                indexDocs(writer, doc);
                            }
                            //                        docList.add(doc);
                            sb = new String();
                            //                        System.out.println(doc.getField("term").stringValue());
                            //                            System.out.println(file.getName());

                        } else {
                            sb += (line);
                        }

                    }
                } catch (IOException ex) {
                    Logger.getLogger(WikiCorpusBuilder.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}

From source file:com.netsteadfast.greenstep.util.UploadSupportUtils.java

/**
 *  (upload) ? map //  w w  w  . jav a  2 s.c  om
 * 
 * @param uploadOid
 * @param tranId
 * @return
 * @throws ServiceException
 * @throws Exception
 */
public static List<Map<String, String>> getTransformSegmentData(String uploadOid, String tranId)
        throws ServiceException, Exception {
    List<Map<String, String>> datas = new LinkedList<Map<String, String>>();
    File file = getRealFile(uploadOid);
    SysUploadTranVO tran = findSysUploadTran(tranId);
    List<TbSysUploadTranSegm> segms = findSysUploadTranSegm(tran.getTranId());
    List<String> txtLines = FileUtils.readLines(file, tran.getEncoding());
    for (int i = 0; i < txtLines.size(); i++) {
        String str = txtLines.get(i);
        if (str.length() < 1) {
            logger.warn("The file: " + file.getPath() + " found zero line.");
            continue;
        }
        if (i < tran.getBeginLen()) { // not begin line.
            continue;
        }
        datas.add(fillDataMap(tran, segms, str));
    }
    Map<String, Object> paramMap = new HashMap<String, Object>();
    paramMap.put(HELP_EXPRESSION_VARIABLE, datas);
    ScriptExpressionUtils.execute(tran.getExprType(), tran.getHelpExpression(), null, paramMap);
    return datas;
}

From source file:com.silverpeas.communicationUser.servlets.AjaxCommunicationUserServlet.java

@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    HttpSession session = req.getSession(true);

    CommunicationUserSessionController commUserSC = (CommunicationUserSessionController) session
            .getAttribute("Silverpeas_" + "communicationUser");
    if (commUserSC != null) {
        String currentUserId = commUserSC.getUserId();

        String action = req.getParameter("Action");
        String userId = req.getParameter("UserIdDest");
        String userId1, userId2;/*w  w  w  .ja  va2  s  . c  o m*/

        Collection<File> listCurrentDiscussion = commUserSC.getListCurrentDiscussion();
        Iterator<File> it = listCurrentDiscussion.iterator();
        File fileDiscussion = null;
        String fileName;
        boolean trouve = false;
        while (it.hasNext() && !trouve) {
            fileDiscussion = it.next();
            fileName = fileDiscussion.getName(); // userId1.userId2.txt
            userId1 = fileName.substring(0, fileName.indexOf('.'));
            userId2 = fileName.substring(fileName.indexOf('.') + 1, fileName.lastIndexOf('.'));
            trouve = ((userId.equals(userId1) && currentUserId.equals(userId2))
                    || (userId.equals(userId2) && currentUserId.equals(userId1)));
        }

        if (!trouve) {
            throw new IOException("Fichier de discussion non trouv !!");
        }
        // Post
        if ("Post".equals(action)) {
            String message = req.getParameter("Msg");
            message = URLDecoder.decode(message, "ISO-8859-1");
            // on client side, javascript function espace do not escape + character.
            // It is manually encoded.
            // So, on server side, it must be decoded manually too !
            message = message.replaceAll("%2B", "+");

            Date now = new Date();
            try {
                commUserSC.addMessageDiscussion(fileDiscussion,
                        "[" + DateUtil.dateToString(now, commUserSC.getLanguage()) + " "
                                + DateUtil.getFormattedTime(now) + "] <"
                                + commUserSC.getUserDetail().getDisplayedName() + "> " + message);
            } catch (CommunicationUserException e) {
                throw new IOException(e.getMessage());
            }
            commUserSC.notifySession(userId, message);
        }

        // Get
        else if ("Get".equals(action)) {
            res.setContentType("text/xml");
            res.setHeader("charset", "UTF-8");

            Writer writer = res.getWriter();
            writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            writer.write("<ajax-response>");
            writer.write("<response type=\"element\" id=\"message\">");
            writer.write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");

            /* lecture du contenu du fichier */
            List<String> lines = FileUtils.readLines(fileDiscussion, "UTF-8");
            userId1 = "<" + commUserSC.getUserDetail().getDisplayedName() + ">"; // <currentUser>
            userId2 = "<" + commUserSC.getUserDetail(userId).getDisplayedName() + ">"; // <User dest>
            String color = "#009900";

            for (String line : lines) {
                writer.write("<tr>");
                if (line.indexOf(userId1) > -1) {
                    color = "#009900";
                } else if (line.indexOf(userId2) > -1) {
                    color = "#cc6600";
                }
                writer.write("<td valign=\"top\"><font color=\"" + color + "\">" + EncodeHelper.escapeXml(line)
                        + "</font></td>");
                writer.write("</tr>");
            }
            writer.write("</table>");
            writer.write("</response>");
            writer.write("</ajax-response>");
        }
        // Clear
        else if ("Clear".equals(action)) {
            try {
                commUserSC.clearDiscussion(fileDiscussion);
            } catch (CommunicationUserException e) {
                throw new IOException(e.getMessage());
            }
            res.setContentType("text/xml");
            res.setHeader("charset", "UTF-8");
            Writer writer = res.getWriter();
            writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            writer.write("<ajax-response>");
            writer.write("<response type=\"element\" id=\"message\">");
            writer.write("<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">");
            writer.write("</table>");
            writer.write("</response>");
            writer.write("</ajax-response>");
        }
    }
}

From source file:eu.delving.services.TestDataSetCycle.java

@Test
public void testHarvestCycle() throws IOException, FileStoreException, MetadataException {
    // import/*from   w  w  w .j a v  a2  s  . c  o  m*/
    Ear importEar = new Ear("Import First Time");
    factory.getDataSetStore().importFile(MockInput.sampleFile(), importEar);
    Assert.assertTrue("import first time", importEar.getResultBoolean());
    Facts facts = Facts.read(new FileInputStream(FACTS_FILE));
    factory.getDataSetStore().setFacts(facts);
    // upload
    DataSetClient client = new DataSetClient(new ClientContext());
    Ear uploadFactsEar = new Ear("UploadFacts");
    client.uploadFile(FileType.FACTS, MockFileStoreFactory.SPEC, FACTS_FILE, uploadFactsEar);
    Assert.assertTrue("upload facts", uploadFactsEar.getResultBoolean());
    Ear uploadSourceEar = new Ear("UploadSource First Time");
    client.uploadFile(FileType.SOURCE, MockFileStoreFactory.SPEC, factory.getDataSetStore().getSourceFile(),
            uploadSourceEar);
    Assert.assertTrue("upload source first time", uploadSourceEar.getResultBoolean());
    // harvest
    Harvester harvester = new Harvester();
    Harvey harvey = new Harvey("first");
    harvester.perform(harvey);
    Assert.assertTrue("harvest", harvey.waitUntilFinished());
    // import again
    importEar = new Ear("Import Again");
    factory.getDataSetStore().importFile(getHarvestedFile("first"), importEar);
    Assert.assertTrue("import again", harvey.waitUntilFinished());
    // change facts
    facts = factory.getDataSetStore().getFacts();
    SourceStream.adjustPathsForHarvest(facts);
    factory.getDataSetStore().setFacts(facts);
    uploadFactsEar = new Ear("UploadFacts Again");
    client.uploadFile(FileType.FACTS, MockFileStoreFactory.SPEC, factory.getDataSetStore().getFactsFile(),
            uploadFactsEar);
    Assert.assertTrue("upload facts", uploadFactsEar.getResultBoolean());
    // upload source again
    uploadSourceEar = new Ear("UploadSource Again");
    client.uploadFile(FileType.SOURCE, MockFileStoreFactory.SPEC, factory.getDataSetStore().getSourceFile(),
            uploadSourceEar);
    Assert.assertTrue("upload source again", uploadSourceEar.getResultBoolean());
    // harvest again
    harvey = new Harvey("again");
    harvester.perform(harvey);
    Assert.assertTrue("harvest again", harvey.waitUntilFinished());
    // compare
    Assert.assertEquals("harvested files different sizes", getHarvestedFile("first").length(),
            getHarvestedFile("again").length());
    List<String> firstLines = FileUtils.readLines(getHarvestedFile("first"), "UTF-8");
    List<String> againLines = FileUtils.readLines(getHarvestedFile("again"), "UTF-8");
    Predicate predicate = new Predicate() {
        @Override
        public boolean evaluate(Object o) {
            return !((String) o).contains("<datestamp>");
        }
    };
    CollectionUtils.filter(firstLines, predicate);
    CollectionUtils.filter(againLines, predicate);
    Assert.assertEquals("lines remaining are different", firstLines.size(), againLines.size());
    for (int walk = 0; walk < firstLines.size(); walk++) {
        Assert.assertEquals("Line " + walk + " different", firstLines.get(walk), againLines.get(walk));
    }
}

From source file:com.legstar.protobuf.cobol.AbstractTest.java

/**
 * Turns the content of a file into a string with platform neutral line
 * separator./*from   w  w  w  .j a v  a2s  .  com*/
 * 
 * @param file the file
 * @return a string with lines separated by "\n"
 * @throws IOException if file cannot be read
 */
public String fileToString(File file) throws IOException {
    List<String> lines = FileUtils.readLines(file, FILES_ENCODING);
    StringBuilder sb = new StringBuilder();
    for (String line : lines) {
        if (sb.length() > 0) {
            sb.append("\n");
        }
        sb.append(line);
    }
    return sb.toString();
}