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.cuhk.hccl.TripAdvisorApp.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 hotelID = fileName.split("_")[1];
    String author = null;/*  w  w w. j a v  a  2 s .  c  o  m*/
    String review = null;
    String[] rates = null;

    for (String line : lines) {
        boolean recordEnd = false;
        try {
            if (line.startsWith("<Author>")) {
                author = line.split(">")[1].trim();
            } else if (line.startsWith("<Content>")) {
                review = line.split(">")[1].trim();
                if (review.isEmpty())
                    continue;
            } else if (line.startsWith("<Rating>")) {
                rates = line.split(">")[1].trim().split("\t");
                // Change missing rating from -1 to 0
                for (int i = 0; i < rates.length; i++) {
                    if (rates[i].equals("-1"))
                        rates[i] = "0";
                }
                recordEnd = true;
            }
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.print(e.getMessage());
            continue;
        }

        if (recordEnd) {
            ArrayList<String> selectedPhrase = DatasetUtil.processRecord(hotelID, result, review, author, rates,
                    Constant.TRIPADVISOR_ASPECTS, 2);

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

    return result;
}

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

private List<String> read(String aFile) throws IOException {
    return FileUtils.readLines(new File(BASE, aFile), "UTF-8");
}

From source file:com.sk89q.warmroast.McpMapping.java

public void read(File joinedFile, File methodsFile) throws IOException {
    try (FileReader r = new FileReader(methodsFile)) {
        try (CSVReader reader = new CSVReader(r)) {
            List<String[]> entries = reader.readAll();
            processMethodNames(entries);
        }/*from  w w  w  .ja  v  a  2  s.  c o m*/
    }

    List<String> lines = FileUtils.readLines(joinedFile, "UTF-8");
    processClasses(lines);
    processMethods(lines);
}

From source file:com.textocat.textokit.eval.cas.FileListCasDirectory.java

@Override
protected IOFileFilter getSourceFileFilter() {
    final Set<String> included = Sets.newHashSet();
    List<String> includedSrcList;
    try {/* w w  w. j  a  va2s  .  c  o  m*/
        includedSrcList = FileUtils.readLines(listFile, "utf-8");
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
    for (String p : includedSrcList) {
        if (StringUtils.isBlank(p)) {
            continue;
        }
        File pFile = new File(p);
        if (pFile.isAbsolute()) {
            included.add(pFile.getAbsolutePath());
        } else {
            included.add(new File(dir, p).getAbsolutePath());
        }
    }
    return FileFilterUtils.asFileFilter(new FileFilter() {
        @Override
        public boolean accept(File f) {
            return included.contains(f.getAbsolutePath());
        }
    });
}

From source file:com.xeiam.datasets.hjabirdsong.bootstrap.RawData2DBTenFold.java

private void go(String dataFile) throws IOException {

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

    for (String line : lines) {

        System.out.println(line);
        Iterable<String> splitLine = Splitter.on(",").split(line);
        Iterator<String> itr = splitLine.iterator();
        String bagid = itr.next();
        String fold = itr.next();
        try {//from  w w  w. jav  a2 s  .  co m
            TenFold tenFold = new TenFold();
            tenFold.setBagid(Integer.parseInt(bagid));
            tenFold.setFold(Integer.parseInt(fold));
            TenFoldDAO.insert(tenFold);
            System.out.println(tenFold.toString());
            idx++;
        } catch (Exception e) {
            // e.printStackTrace();
            // eat it. skip first line in file.
        }

    }

    System.out.println("Number parsed: " + idx);

}

From source file:com.github.stagirs.lingvo.syntax.disambiguity.MyStemTest.java

@Before
public void before() throws IOException {
    for (String line : FileUtils.readLines(new File("mystemMapping"), "utf-8")) {
        String[] parts = line.split(",");
        List<String> attrs = new ArrayList<String>();
        for (int i = 2; i < parts.length; i++) {
            if (parts[i].isEmpty()) {
                continue;
            }/*from   w ww  . j  a v  a  2s.c  o m*/
            attrs.add(parts[i]);
        }
        opencorpora2mystem.put(Attr.valueOf(parts[0]), attrs);
    }
}

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

public static void importLogPywws(Logger log) {
    try {/*from ww w  .jav a2 s.c  o m*/

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

        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());

        }

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

    }
}

From source file:android.databinding.tool.util.XmlEditor.java

public static String strip(File f, String newTag) throws IOException {
    ANTLRInputStream inputStream = new ANTLRInputStream(new FileReader(f));
    XMLLexer lexer = new XMLLexer(inputStream);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    XMLParser parser = new XMLParser(tokenStream);
    XMLParser.DocumentContext expr = parser.document();
    XMLParser.ElementContext root = expr.element();

    if (root == null || !"layout".equals(nodeName(root))) {
        return null; // not a binding layout
    }//from   w  ww . ja v  a2  s  .co  m

    List<? extends ElementContext> childrenOfRoot = elements(root);
    List<? extends XMLParser.ElementContext> dataNodes = filterNodesByName("data", childrenOfRoot);
    if (dataNodes.size() > 1) {
        L.e("Multiple binding data tags in %s. Expecting a maximum of one.", f.getAbsolutePath());
    }

    ArrayList<String> lines = new ArrayList<>();
    lines.addAll(FileUtils.readLines(f, "utf-8"));

    for (android.databinding.parser.XMLParser.ElementContext it : dataNodes) {
        replace(lines, toPosition(it.getStart()), toEndPosition(it.getStop()), "");
    }
    List<? extends XMLParser.ElementContext> layoutNodes = excludeNodesByName("data", childrenOfRoot);
    if (layoutNodes.size() != 1) {
        L.e("Only one layout element and one data element are allowed. %s has %d", f.getAbsolutePath(),
                layoutNodes.size());
    }

    final XMLParser.ElementContext layoutNode = layoutNodes.get(0);

    ArrayList<Pair<String, android.databinding.parser.XMLParser.ElementContext>> noTag = new ArrayList<>();

    recurseReplace(layoutNode, lines, noTag, newTag, 0);

    // Remove the <layout>
    Position rootStartTag = toPosition(root.getStart());
    Position rootEndTag = toPosition(root.content().getStart());
    replace(lines, rootStartTag, rootEndTag, "");

    // Remove the </layout>
    ImmutablePair<Position, Position> endLayoutPositions = findTerminalPositions(root, lines);
    replace(lines, endLayoutPositions.left, endLayoutPositions.right, "");

    StringBuilder rootAttributes = new StringBuilder();
    for (AttributeContext attr : attributes(root)) {
        rootAttributes.append(' ').append(attr.getText());
    }
    Pair<String, XMLParser.ElementContext> noTagRoot = null;
    for (Pair<String, XMLParser.ElementContext> pair : noTag) {
        if (pair.getRight() == layoutNode) {
            noTagRoot = pair;
            break;
        }
    }
    if (noTagRoot != null) {
        ImmutablePair<String, XMLParser.ElementContext> newRootTag = new ImmutablePair<>(
                noTagRoot.getLeft() + rootAttributes.toString(), layoutNode);
        int index = noTag.indexOf(noTagRoot);
        noTag.set(index, newRootTag);
    } else {
        ImmutablePair<String, XMLParser.ElementContext> newRootTag = new ImmutablePair<>(
                rootAttributes.toString(), layoutNode);
        noTag.add(newRootTag);
    }
    //noinspection NullableProblems
    Collections.sort(noTag, new Comparator<Pair<String, XMLParser.ElementContext>>() {
        @Override
        public int compare(Pair<String, XMLParser.ElementContext> o1,
                Pair<String, XMLParser.ElementContext> o2) {
            Position start1 = toPosition(o1.getRight().getStart());
            Position start2 = toPosition(o2.getRight().getStart());
            int lineCmp = Integer.compare(start2.line, start1.line);
            if (lineCmp != 0) {
                return lineCmp;
            }
            return Integer.compare(start2.charIndex, start1.charIndex);
        }
    });
    for (Pair<String, android.databinding.parser.XMLParser.ElementContext> it : noTag) {
        XMLParser.ElementContext element = it.getRight();
        String tag = it.getLeft();
        Position endTagPosition = endTagPosition(element);
        fixPosition(lines, endTagPosition);
        String line = lines.get(endTagPosition.line);
        String newLine = line.substring(0, endTagPosition.charIndex) + " " + tag
                + line.substring(endTagPosition.charIndex);
        lines.set(endTagPosition.line, newLine);
    }
    return StringUtils.join(lines, System.getProperty("line.separator"));
}

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

@Override
public void run(String[] args) throws IOException {
    super.run(args);

    // Put into itemSet once all business_ids for some category (e.g. restaurant) from a file
    String businessFile = cmdLine.getOptionValue('b');
    String category = cmdLine.getOptionValue('c');

    if (itemSet.isEmpty()) {
        try {//from  ww  w  .ja v  a 2 s  . co m
            List<String> lines = FileUtils.readLines(new File(businessFile), "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();
        }
    }

    // Process data file and save result
    String dataFileName = cmdLine.getOptionValue('f');
    String result = processStream(dataFileName).toString();
    FileUtils.writeStringToFile(outFile, result, false);

    System.out.println("Processing is fininshed!");
}

From source file:de.julielab.jtbd.TokenizerTest.java

private List<String> readLinesFromFile(final String filename) throws IOException {
    return FileUtils.readLines(new File(filename), "utf-8");
}