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:edu.ku.brc.specify.toycode.ResFileCompare.java

@SuppressWarnings("unchecked")
public void fixPropertiesFiles(final String baseFileName, final String lang, final boolean doBranch) {
    System.out.println("-------------------- " + baseFileName + " --------------------");
    File engFile;//www  .java  2 s. co  m
    File lngFile;

    String engName = String.format("src/%s_en.properties", baseFileName);
    String langName = String.format("src/%s_%s.properties", baseFileName, lang);

    if (doBranch) {
        engFile = new File(
                String.format("/home/rods/workspace/Specify_6202SF/src/%s_en.properties", baseFileName));
        lngFile = new File(
                String.format("/home/rods/workspace/Specify_6202SF/src/%s_%s.properties", baseFileName, lang));
    } else {
        engFile = new File(engName);
        lngFile = new File(langName);
    }

    try {
        List<String> engList = (List<String>) FileUtils.readLines(engFile, "UTF8");
        List<String> lngListTmp = (List<String>) FileUtils.readLines(lngFile, "UTF8");

        int lineCnt = -1;
        HashMap<String, String> transHash = new HashMap<String, String>();
        for (String line : lngListTmp) {
            lineCnt++;

            if (line.startsWith("#") || StringUtils.deleteWhitespace(line).length() < 3
                    || line.indexOf('=') == -1) {
                continue;
            }

            String[] toks = StringUtils.split(line, '=');
            if (toks.length > 1) {
                if (toks.length == 2) {
                    transHash.put(toks[0], toks[1]);

                } else {
                    StringBuilder sb = new StringBuilder();
                    for (int i = 1; i < toks.length; i++) {
                        sb.append(String.format("%s=", toks[i]));
                    }
                    sb.setLength(sb.length() - 1); // chomp extra '='
                    transHash.put(toks[0], sb.toString());
                }
            } else {
                log.error("Skipping:[" + line + "] Line:" + lineCnt);
            }
        }

        log.info(String.format("Lines Eng: %d;  Terms Hash size: %s: %d", engList.size(), lang,
                transHash.size()));

        File dir = new File("translations");
        if (!dir.exists()) {
            if (!dir.mkdir()) {
                log.error("Unable to create directory[" + dir.getAbsolutePath() + "]");
                return;
            }
        }

        File transFile = new File(dir.getPath() + File.separator + langName.substring(4));
        PrintWriter transFileOutput = new PrintWriter(transFile, "UTF8");

        for (String line : engList) {
            if (line.startsWith("#") || StringUtils.deleteWhitespace(line).length() < 3
                    || line.indexOf('=') == -1) {
                transFileOutput.println(line);
                continue;
            }

            boolean doMove = true;
            String[] toks = StringUtils.split(line, '=');
            if (toks.length > 1) {
                String key = null;
                String value = null;
                if (toks.length == 2) {
                    key = toks[0];
                    value = toks[1];

                } else {
                    key = toks[0];
                    StringBuilder sb = new StringBuilder();
                    for (int i = 1; i < toks.length; i++) {
                        sb.append(String.format("%s=", toks[i]));
                    }
                    sb.setLength(sb.length() - 1); // chomp extra '='
                    value = sb.toString();
                }

                if (key != null) {
                    String text = transHash.get(key);
                    transFileOutput.println(String.format("%s=%s", key, text != null ? text : value));

                    if (text == null) {
                        log.info("Adding new term: " + key);
                    }
                    doMove = false;
                } else {
                    log.info("Adding new term: " + key);
                }
            }

            if (doMove) {
                transFileOutput.println(line);
            }
        }

        transFileOutput.flush();
        transFileOutput.close();

        log.info(String.format("Write file: %s", transFile.getPath()));

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

From source file:edu.cuhk.hccl.hadoop.YelpMapper.java

private void readItemSet(Path filePath, String category) {
    if (itemSet.isEmpty()) {
        try {//from w w  w.  j  a  va 2  s . com
            List<String> lines = FileUtils.readLines(new File(filePath.toString()), "UTF-8");
            for (String line : lines) {
                Business business = gson.fromJson(line, Business.class);

                boolean isIn = false;
                for (String cate : business.categories) {
                    if (category.equals(cate.toLowerCase())) {
                        isIn = true;
                        break;
                    }
                }

                if (isIn) {
                    if (!itemSet.contains(business.business_id)) {
                        itemSet.add(business.business_id);
                    }
                }
            }

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

From source file:de.tudarmstadt.ukp.csniper.resbuild.stuff.BncLocalCorpusBuilder.java

private static List<String> read(String aFile, String aPatternPrefix) throws IOException {
    List<String> patterns = new ArrayList<String>();
    for (String s : FileUtils.readLines(new File(aFile), "UTF-8")) {
        patterns.add(aPatternPrefix + s);
    }//from   w  w w  .  j a v  a  2 s.  c o m
    return patterns;
}

From source file:com.bluexml.tools.miscellaneous.Translate.java

public static void writeBackValues(File values, File propertiesFile) throws IOException {
    List<String> readLines = FileUtils.readLines(values, "UTF-8");
    Properties props = new Properties();
    TreeMap<String, String> propsMap = loadProperties(propertiesFile);
    Set<String> keySet = propsMap.keySet();
    int index = 0;
    for (String key : keySet) {
        String value = readLines.get(index);
        System.out.println("before trans :" + value);

        System.out.println("after trans :" + value);
        System.out.println();//from   www .  j  a  v a  2 s.  c o  m
        props.setProperty(key, value);
        index++;
    }

    FileOutputStream out = new FileOutputStream(propertiesFile);
    props.store(out, null);
    out.close();

}

From source file:core.test.server.mock.util.PersonNameUtil.java

/**
 * read last names from file and populate array
 * @throws IOException //from  w w  w .ja v  a  2s .com
 */
private void initializeLastNames() throws IOException {
    URL url = getClass().getClassLoader().getResource("people-names/last-names.txt");
    File file = new File(url.getFile());
    lastNames = FileUtils.readLines(file, "UTF-8");
}

From source file:com.xiaomi.linden.server.TestLindenServer.java

public static void index() throws IOException {
    String dataFile = TestLindenServer.class.getClassLoader().getResource(INDEX_FILE).getFile();
    List<String> lines = FileUtils.readLines(new File(dataFile), Charset.forName("UTF-8"));
    for (String line : lines) {
        JSONObject indexRequestJSON = new JSONObject();
        indexRequestJSON.put("type", "index");
        JSONObject json = JSONObject.parseObject(line);
        String sName = json.getString("sName");
        // sNameStored has same value with sName, but different field type
        json.put("sNameStored", sName);
        indexRequestJSON.put("content", json);
        try {/*from   w  ww.j  a  v a 2s .  co  m*/
            client1.index(indexRequestJSON.toJSONString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    JSONObject command = new JSONObject();
    command.put("type", "MERGE_INDEX");
    JSONObject options = new JSONObject();
    options.put("count", 1);
    command.put("options", options);
    try {
        client1.executeCommand(command.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:eu.annocultor.utils.HierarchyTracingFilter.java

public void doTracing(String fileWithTopConcepts, String fileWithSelection, String... fileWithRdf)
        throws Exception {
    Repository rdf = Helper.createLocalRepository();
    File[] filesToLoad = new File[fileWithRdf.length];
    for (int i = 0; i < fileWithRdf.length; i++) {
        filesToLoad[i] = new File(fileWithRdf[i]);
    }/*w  w  w  . j  a  va  2s . com*/
    Helper.importRDFXMLFile(rdf, Namespaces.ANNOCULTOR_CONVERTER.getUri(), filesToLoad);
    RepositoryConnection connection = rdf.getConnection();
    ValueFactory factory = rdf.getValueFactory();

    System.out.println("Loaded " + connection.size() + " statements");
    List topConceptsStr = FileUtils.readLines(new File(fileWithTopConcepts), "UTF-8");
    List<StringInStack> top = new ArrayList<StringInStack>();
    for (Object object : topConceptsStr) {
        top.add(new StringInStack(object.toString(), 0));
    }

    Set<String> passedUrls = traceBroaderDepthFirst(connection, factory, top);

    // save
    List<String> passedSorted = new LinkedList<String>();
    passedSorted.addAll(passedUrls);
    Collections.sort(passedSorted);
    System.out.println("Saving " + passedSorted.size() + " terms");
    saveListOfUrls(fileWithSelection, passedUrls, passedSorted);
}

From source file:com.textocat.textokit.commons.cpe.XmiFileListReader.java

@Override
protected Iterable<Resource> getResources(UimaContext ctx) throws IOException {
    String baseDirPath = this.baseDirPath;
    baseDirPath = FilenameUtils.normalize(baseDirPath);
    // ensure that baseDirPath ends with slash for proper relative path handling
    if (!baseDirPath.endsWith(File.separator)) {
        baseDirPath += File.separator;
    }//  www . jav a  2s .c o m
    baseDir = new FileSystemResource(baseDirPath);
    if (!baseDir.exists()) {
        throw new IllegalStateException(String.format("Directory %s does not exist", baseDir));
    }
    List<String> lines = FileUtils.readLines(listFile, "utf-8");
    resources = Lists.transform(Lists.newArrayList(Iterables.filter(lines, notBlankString)),
            relativeFileResourceFunc);
    return resources;
}

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

public void process(final SeleCrawlerSettings settings) throws IOException {
    this.settings = settings;
    System.err.println("[jp.igapyon.selecrawler] Analyze web contents.");

    final List<File> files = new SimpleDirParser() {
        public boolean isProcessTarget(final File file) {
            if (file.isDirectory()) {
                return true;
            }//  w w  w  .ja  v  a  2 s. c  om
            if (file.getName().endsWith(SeleCrawlerConstants.EXT_SC_URL)) {
                return true;
            }
            return false;
        }
    }.listFiles(new File(settings.getPathTargetDir()), true);

    System.err.println("[selecrawler] create/update '*" + SeleCrawlerConstants.EXT_SC_HEAD + "' and '*"
            + SeleCrawlerConstants.EXT_SC_ANCHOR + "' files.");
    for (File fileMeta : files) {
        if (fileMeta.isDirectory()) {
            continue;
        }
        final List<String> metaUrlList = FileUtils.readLines(fileMeta, "UTF-8");

        final File file = new File(fileMeta.getParentFile(), fileMeta.getName().substring(0,
                fileMeta.getName().length() - SeleCrawlerConstants.EXT_SC_URL.length()));

        processFile(file, metaUrlList.get(1));
    }
}

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

public void processDevice(final String deviceName) throws IOException {
    System.err.println("[selecrawler] Launch Chrome. UA:" + deviceName);
    final SimpleChromeWrapper chrome = new SimpleChromeWrapper(settings.getPathChromeDriver(), deviceName,
            settings.getPathUserDataDir());
    chrome.open();/*from w w  w . j  a va2s  .  co m*/

    int getcounter = 0;

    System.err.println(
            "[selecrawler] Load url list file: " + new File(settings.getPathUrllisttTxt()).getCanonicalPath());
    System.err.println("[selecrawler] Target dir: " + new File(settings.getPathTargetDir()).getCanonicalPath());

    final List<String> urls = FileUtils.readLines(new File(settings.getPathUrllisttTxt()), "UTF-8");
    for (String urlLookup : urls) {
        if (getcounter >= 10) {
            // refresh chrome instance
            getcounter = 0;
            chrome.close();
            chrome.open();
        }

        final File outputFile = getFileHtml(deviceName, urlLookup);
        if (outputFile.getParentFile().exists() == false) {
            outputFile.getParentFile().mkdirs();
        }

        final File outputMetaFile = new File(outputFile.getParentFile(),
                outputFile.getName() + SeleCrawlerConstants.EXT_SC_URL);

        final File outputLogFile = new File(outputFile.getParentFile(),
                outputFile.getName() + SeleCrawlerConstants.EXT_SC_LOG);

        if (outputMetaFile.exists()) {
            if (settings.isDebug()) {
                System.err.println("[selecrawler] skip(cache): " + urlLookup);
            }
            continue;
        }

        System.err.println("[selecrawler] fetch web: " + urlLookup);

        chrome.getDriver().get(urlLookup);

        {
            // check wait settings.
            final String urlActual = chrome.getDriver().getCurrentUrl();
            for (String regex : settings.getUrllistWaitRegex()) {
                final Pattern pat = Pattern.compile(regex);
                final Matcher mat = pat.matcher(urlActual);
                if (mat.find()) {
                    try {
                        System.out.println("waiting browser operation");
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        final String contents = chrome.getDriver().getPageSource();
        FileUtils.writeStringToFile(outputFile, contents, "UTF-8");

        FileUtils.writeLines(outputLogFile, "UTF-8", chrome.getLogEntries());

        // write meta finally.
        {
            final List<String> metaUrlList = new ArrayList<String>();
            metaUrlList.add(urlLookup);
            metaUrlList.add(chrome.getDriver().getCurrentUrl());
            FileUtils.writeLines(outputMetaFile, "UTF-8", metaUrlList);
        }
        getcounter++;

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    chrome.close();
}