All Downloads are FREE. Search and download functionalities are using the official Maven repository.

uk.ac.shef.dcs.sti.util.FileUtils Maven / Gradle / Ivy

The newest version!
package uk.ac.shef.dcs.sti.util;

import org.apache.commons.io.LineIterator;

import java.io.*;
import java.util.*;

/**
 * Several utility methods related to files.
 *
 * @author Ziqi Zhang
 */
public class FileUtils {


    /**
     * Read input raw text file as a list
     *
     * @param path      input file path
     * @param lowercase whether to convert input string to lowercase
     * @return
     * @throws IOException
     */
    public static List readList(final String path, final boolean lowercase) throws IOException {
        List res = new ArrayList<>();
        LineIterator it = org.apache.commons.io.FileUtils.lineIterator(new File(path));
        while(it.hasNext()){
            String line=it.nextLine().trim();
            if (line.equals("")) continue;
            if (lowercase) res.add(line.toLowerCase());
            else res.add(line);
        }

        return res;
    }

    public static List readList(final String path, final boolean lowercase, final String charset) throws IOException {
        List res = new ArrayList<>();
        final InputStreamReader ir = new InputStreamReader(new FileInputStream(path), charset);

        final BufferedReader reader = new BufferedReader(ir);
        String line;
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.equals("")) continue;
            if (lowercase) res.add(line.toLowerCase());
            else res.add(line);
        }

        reader.close();
        return res;
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy