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:de.tudarmstadt.ukp.dkpro.core.textnormalizer.annotations.AnnotationByTextFilter.java

private void readWords() throws IOException {
    words = new HashSet<>();
    for (String line : FileUtils.readLines(modelLocation, modelEncoding)) {
        words.add(ignoreCase ? line.trim().toLowerCase() : line.trim());
    }//from w w w .j av a 2 s . co  m
}

From source file:de.tudarmstadt.ukp.dkpro.tc.features.style.TopicWordsFeatureExtractor.java

private List<Feature> countWordHits(String wordListName, List<String> tokens)
        throws TextClassificationException {

    // word lists are stored in resources folder relative to feature extractor
    String wordListPath = TopicWordsFeatureExtractor.class.getClassLoader().getResource("./" + wordListName)
            .getPath();/*from  w ww. ja  va  2s  . c o m*/
    List<String> topicwords = null;
    try {
        topicwords = FileUtils.readLines(new File(wordListPath), "utf-8");
    } catch (IOException e) {
        throw new TextClassificationException(e);
    }
    int wordcount = 0;
    for (String token : tokens) {
        if (topicwords.contains(token)) {
            wordcount++;
        }
    }
    double numTokens = tokens.size();
    // name the feature same as wordlist
    return Arrays.asList(new Feature(prefix + wordListName, numTokens > 0 ? (wordcount / numTokens) : 0));
}

From source file:edu.cuhk.hccl.YelpApp.java

@Override
public StringBuilder processStream(String fileName) throws FileNotFoundException, IOException {
    StringBuilder result = new StringBuilder();

    List<String> lines = FileUtils.readLines(new File(fileName), "UTF-8");

    String[] rates = new String[4];

    for (String line : lines) {
        Review review = gson.fromJson(line, Review.class);

        if (review.text.isEmpty() || !itemSet.contains(review.getBusinessId()))
            continue;

        String author = review.user_id;
        String itemId = review.business_id;
        rates[0] = review.stars;/* w w  w.ja va 2 s.  com*/
        rates[1] = review.votes.funny;
        rates[2] = review.votes.useful;
        rates[3] = review.votes.cool;

        ArrayList<String> selectedPhrase = DatasetUtil.processRecord(itemId, result, review.text, author, rates,
                Constant.RESTAURANT_ASPECTS, 2);

        for (String phrase : selectedPhrase) {
            FileUtils.writeStringToFile(phraseFile, phrase + "\n", true);
        }
    }

    return result;
}

From source file:net.nifheim.beelzebu.coins.common.utils.FileManager.java

private void updateConfig() {
    try {/*from   w  w w. j  ava  2 s .  c o m*/
        List<String> lines = FileUtils.readLines(configFile, Charsets.UTF_8);
        int index;
        if (core.getConfig().getInt("version") == 13) {
            core.log("The config file is up to date.");
        } else {
            switch (core.getConfig().getInt("version")) {
            case 9:
                index = lines.indexOf("MySQL:") - 2;
                lines.addAll(index, Arrays.asList(
                        "# Here you can enable Vault to make this plugin manage all the Vault transactions.",
                        "Vault:", "  Use: false", "  # Names used by vault for the currency.", "  Name:",
                        "    Singular: 'Coin'", "    Plural: 'Coins'", ""));
                index = lines.indexOf("version: 9");
                lines.set(index, "version: 10");
                core.log("Configuraton file updated to v10");
                break;
            case 10:
                index = lines.indexOf("    Close:") + 1;
                lines.addAll(index, Arrays.asList(
                        "      # To see all possible values check https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Material.html",
                        "      Material: REDSTONE_BLOCK", "      Name: '&c&lClose'", "      Lore:",
                        "      - ''", "      - '&7Click me to close this gui'"));
                index = lines.indexOf("version: 10");
                lines.set(index, "version: 11");
                core.log("Configuraton file updated to v11");
                break;
            case 11:
                index = lines.indexOf("  Executor Sign:") + 5;
                lines.addAll(index, Arrays.asList(
                        "  # If you want the users to be created when they join to the server, enable this,",
                        "  # otherwise the players will be created when his coins are modified or consulted",
                        "  # to the database for the first time (recommended for big servers).",
                        "  Create Join: false"));
                index = lines.indexOf("version: 11");
                lines.set(index, "version: 12");
                core.log("Configuration file updated to v12");
                break;
            case 12:
                index = lines.indexOf("version: 12");
                lines.set(index, "version: 13");
                core.log("Configuration file updated to v13");
                break;
            default:
                core.log(
                        "Seems that you hava a too old version of the config or you canged this to another number >:(");
                core.log(
                        "We can't update it, if is a old version you should try to update it slow and not jump from a version to another, keep in mind that we keep track of the last 3 versions of the config to update.");
                break;
            }
        }
        FileUtils.writeLines(configFile, lines);
        core.getConfig().reload();
    } catch (IOException ex) {
        core.log("An unexpected error occurred while updating the config file.");
        core.debug(ex.getMessage());
    }
}

From source file:de.tudarmstadt.ukp.csniper.webapp.search.cqp.CqpEngine.java

public String getEncoding(String aCollectionId) {
    try {// www. j av  a  2 s  .c  o m
        List<String> lines = FileUtils.readLines(new File(getRegistryPath(), aCollectionId.toLowerCase()),
                "UTF-8");
        for (String line : lines) {
            line = line.toLowerCase();
            if (line.startsWith("##:: charset")) {
                if (line.contains("iso-8859-1") || line.contains("latin1")) {
                    return "ISO-8859-1";
                }
                break;
            }
        }
        return "UTF-8";
    } catch (IOException e) {
        throw new DataAccessResourceFailureException("Unable to read registry file", e);
    }
}

From source file:com.screenslicer.common.CommonFile.java

public static List<String> readLines(File file) {
    List<String> content = null;
    synchronized (lock(file.getAbsolutePath())) {
        try {// w w  w .  ja  va 2s.co m
            content = FileUtils.readLines(file, "utf-8");
        } catch (Throwable t) {
            Log.exception(t);
        }
    }
    unlock(file.getAbsolutePath());
    return content;
}

From source file:com.github.cereda.arara.rulechecker.RuleUtils.java

/**
 * Reads a list of files and returns a list of reports of each rule.
 * @param files A list of files./*from  www .java2 s  .  c o  m*/
 * @return A list of reports of each rule.
 */
public static List<RuleReport> readRules(List<File> files) {

    // check if the provided list is empty
    if (files.isEmpty()) {

        // print error message
        System.err.println(WordUtils.wrap("Fatal exception: I could not find any rules in "
                + "the provided directory. I am afraid I won't be "
                + "be able to continue. Please make sure the "
                + "provided directory contains at least one rule to "
                + "be analyzed. The application will halt now.", 60));
    }

    // the resulting list
    List<RuleReport> reports = new ArrayList<>();

    // read each file of the list and extract
    // each task found
    for (File file : files) {

        try {

            // read each file into a list
            // of strings
            List<String> lines = FileUtils.readLines(file, "UTF-8");

            // create a list of pair of tasks
            List<Pair<Boolean, String>> tasks = new ArrayList<>();

            // iterate through each line
            // of the current file
            for (String line : lines) {

                // try to extract the task and
                // add it to the list of tasks
                update(line, tasks);

            }

            // create a new report
            RuleReport rule = new RuleReport(file);

            // add the list of tasks to
            // this report
            rule.setTasks(tasks);

            // and add the report
            // to the list
            reports.add(rule);

        } catch (IOException exception) {

            // print error message
            System.err.println(WordUtils.wrap("Fatal exception: an error was raised while "
                    + "trying to read one of the rules. Please make "
                    + "sure all rules in the provided directory have "
                    + "read permission. I won't be able to continue. " + "The application will halt now.", 60));
            System.exit(1);
        }
    }

    // return the list of
    // analyzed rules
    return reports;
}

From source file:com.roncoo.pay.app.reconciliation.parser.WEIXINParser.java

/**
 * ????/*from   ww  w .j  a v a2  s .co  m*/
 * 
 * @param file
 *            ??
 * @param billDate
 *            ?
 * @param batch
 *            
 * @return
 */
public List<ReconciliationEntityVo> parser(File file, Date billDate, RpAccountCheckBatch batch) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
    String billDateStr = sdf.format(billDate);

    // file?
    this.isError(file, batch);
    if (batch.getStatus() != null) {
        if (batch.getStatus().equals(BatchStatusEnum.ERROR.name())
                || batch.getStatus().equals(BatchStatusEnum.NOBILL.name())) {
            if (LOG.isDebugEnabled()) {
                LOG.debug(", : " + billDateStr + ", batchStatus: "
                        + BatchStatusEnum.ERROR + ", bankMsg: [" + batch.getBankErrMsg() + "], checkFailMsg: ["
                        + batch.getCheckFailMsg() + "]");
            }
            return null;
        }
    }
    // file??
    try {
        List<String> list = FileUtils.readLines(file, "UTF-8");
        // 
        for (Iterator<String> it = list.iterator(); it.hasNext();) {
            if (StringUtils.isBlank(it.next())) {
                it.remove();
            }
        }

        List<ReconciliationEntityVo> sheetList = null;
        sheetList = parseSuccess(list, billDateStr, batch);
        return sheetList;
    } catch (IOException e) {
        LOG.error("??", e);
        return null;
    }

}

From source file:it.marcoberri.mbmeteo.action.Commons.java

/**
 *
 * @param log//from   w w  w.j  a va 2s  . c  o m
 */
public static void importLogEasyWeather(Logger log) {

    try {

        final File importPath = new File(
                ConfigurationHelper.prop.getProperty("import.loggerEasyWeather.filepath"));
        FileUtils.forceMkdir(importPath);
        final File importPathBackup = new File(
                ConfigurationHelper.prop.getProperty("import.loggerEasyWeather.filepath") + File.separator
                        + "old" + File.separator);
        FileUtils.forceMkdir(importPathBackup);
        boolean hasHeader = Default
                .toBoolean(ConfigurationHelper.prop.getProperty("import.loggerEasyWeather.hasHeader"), true);

        final String[] extension = { "csv", "txt" };
        Collection<File> files = FileUtils.listFiles(importPath, extension, false);

        if (files.isEmpty()) {
            log.debug("No file to inport: " + importPath);
            return;
        }

        for (File f : files) {

            log.debug("read file:" + f);

            final List<String> l = FileUtils.readLines(f, "UTF-8");
            log.debug("tot line:" + l.size());

            final Datastore ds = MongoConnectionHelper.ds;

            log.debug("hasHeader: " + hasHeader);
            int lineCount = 0;
            int lineDouble = 0;
            for (String s : l) {

                if (hasHeader) {
                    hasHeader = false;
                    continue;
                }

                final String[] columns = s.split(";");
                List<Meteolog> check = ds.createQuery(Meteolog.class).field("time")
                        .equal(getDate("dd-MM-yyyy HH:mm", columns[1], log)).asList();

                if (check != null && !check.isEmpty()) {
                    log.debug("data exist, continue");
                    lineDouble++;
                    continue;
                }

                final Meteolog meteoLog = new Meteolog();
                try {
                    meteoLog.setN(Integer.valueOf(columns[0]));
                } catch (Exception e) {
                    log.error("line skipped " + lineCount, e);
                    continue;
                }

                try {
                    meteoLog.setTime(getDate("dd-MM-yyyy HH:mm", columns[1], log));
                } catch (Exception e) {
                    log.error("line skipped" + lineCount, e);
                    continue;
                }

                try {
                    meteoLog.setInterval(Integer.valueOf(columns[2]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setIndoorHumidity(Double.valueOf(columns[3]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setIndoorTemperature(Double.valueOf(columns[4]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setOutdoorHumidity(Double.valueOf(columns[5]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setOutdoorTemperature(Double.valueOf(columns[6]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setAbsolutePressure(Double.valueOf(columns[7]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setWind(Double.valueOf(columns[8]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setGust(Double.valueOf(columns[9]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setDirection(columns[10]);
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setRelativePressure(Double.valueOf(columns[11]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setDewpoint(Double.valueOf(columns[12]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setWindChill(Double.valueOf(columns[13]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setHourRainfall(Double.valueOf(columns[14]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setDayRainfall(Double.valueOf(columns[15]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setWeekRainfall(Double.valueOf(columns[16]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setMonthRainfall(Double.valueOf(columns[17]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setTotalRainfall(Double.valueOf(columns[18]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setWindLevel(Double.valueOf(columns[19]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                try {
                    meteoLog.setGustLevel(Double.valueOf(columns[20]));
                } catch (Exception e) {
                    log.error("line " + lineCount, e);
                }

                ds.save(meteoLog, WriteConcern.NORMAL);
                lineCount++;

            }

            log.debug("Tot line insert:" + lineCount);
            log.debug("Tot line scarted:" + lineDouble);

            //move file to backup dir with rename
            final File toMove = new File(
                    importPathBackup + "/" + f.getName() + "_" + System.currentTimeMillis());

            log.debug("Move File to Backup dir" + toMove);
            f.renameTo(toMove);

        }
    } catch (IOException ex) {
        log.fatal(ex);

    }
}

From source file:de.meisl.eisstockitems.ImportTest.java

@Test
public void importJahreskennbuchstaben() throws IOException, ParseException {
    List<String> readLines = FileUtils.readLines(new File("data/jahreskennbuchstaben.csv"), "UTF-8");
    for (String line : readLines) {

        DateFormat format = new SimpleDateFormat("dd.MM.yyyy", Locale.GERMAN);

        String[] split = line.split(",", -1);
        Date year = format.parse(split[0]);
        String character = split[1];
        String remark = split[2];

        List<YearCharacter> category = yearCharRepository.findByYear(year);
        if (category.isEmpty()) {
            log.info(String.format("Add new YearCharacter '%s' for year '%s'", character, year));
            YearCharacter item = new YearCharacter();
            item.setYearCharacter(character);
            item.setYear(year);/*from   w  ww . ja  v a2  s  .c o m*/
            item.setRemark(remark);

            item = yearCharRepository.save(item);

            log.info("Created YearCharacter for RegNr '" + character + "': " + item);
        }
    }

}