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) throws IOException 

Source Link

Document

Reads the contents of a file line by line to a List of Strings using the default encoding for the VM.

Usage

From source file:com.ms.commons.test.tool.util.AutoDeleteProjectTaskUtil.java

public static Task wrapAutoDeleteTask(final File project, final Task oldTask) {

    String userDir = System.getProperty("user.dir");

    List<Element> projectElements = ClassPathAccessor.getElementsByXPath(project, "/project");
    if ((projectElements == null) || (projectElements.size() != 1)) {
        System.err.println("File '" + project + "' format error!");
        System.exit(-1);// w w w. j  a v a2s .  co  m
    }
    final String projectId = projectElements.get(0).getAttributeValue("id");
    final String projectExtends = projectElements.get(0).getAttributeValue("extends");

    System.out.println("Project id:" + projectId);
    System.out.println("Project extends:" + projectExtends);

    final File baseProject = new File(userDir + File.separator + projectExtends);
    if (!baseProject.exists()) {
        System.err.println("Base file '" + baseProject + "' not found!");
        System.exit(-1);
    }

    List<Element> findProjectIdElements = ClassPathAccessor.getElementsByXPath(baseProject,
            "/project/projects/project[@id='" + projectId + "']");

    if ((findProjectIdElements != null) && (findProjectIdElements.size() > 0)) {
        System.out.println("Find project id in base project file.");
        return new Task() {

            @SuppressWarnings("unchecked")
            public void finish() {
                boolean hasError = false;
                File backUpFile = new File(baseProject.getAbsoluteFile() + ".backup");
                try {
                    FileUtils.copyFile(baseProject, backUpFile);

                    // XMLAPI
                    List<String> outLines = new ArrayList<String>();
                    List<String> lines = FileUtils.readLines(baseProject);
                    for (String line : lines) {
                        if (!(line.contains("\"" + projectId + "\""))) {
                            outLines.add(line);
                        }
                    }
                    FileUtils.writeLines(baseProject, outLines);

                    oldTask.finish();
                } catch (Exception e) {
                    hasError = true;
                    e.printStackTrace();
                } finally {
                    baseProject.delete();
                    try {
                        FileUtils.copyFile(backUpFile, baseProject);
                    } catch (IOException e) {
                        hasError = true;
                        e.printStackTrace();
                    }
                    backUpFile.delete();
                }
                if (hasError) {
                    System.exit(-1);
                }
            }
        };
    } else {
        System.out.println("Not find project id in base project file.");
    }

    return oldTask;
}

From source file:de.tudarmstadt.ukp.dkpro.wsd.si.dictionary.util.UkbDictionary.java

public UkbDictionary(String path, String neededMentionsPath) throws FileNotFoundException, IOException {

    HashSet<String> neededMentions = new HashSet<String>(FileUtils.readLines(new File(neededMentionsPath)));

    mentionMap = new HashMap<String, List<String[]>>();
    targetMap = new HashMap<String, Integer>();

    BufferedReader reader = new BufferedReader(
            new InputStreamReader(new BZip2CompressorInputStream(new FileInputStream(path))));

    String line;//from  ww w  .j  a va2  s .  co m
    String[] lineArray;
    List<String[]> entities;
    //      Timer timer = new Timer(9615853);
    while ((line = reader.readLine()) != null) {

        lineArray = line.split(" ");

        if (neededMentions.contains(lineArray[0].toLowerCase())) {
            entities = new LinkedList<String[]>();
            for (int i = 1; i < lineArray.length; i++) {

                String target = lineArray[i].substring(0, lineArray[i].lastIndexOf(":"));
                String frequency = lineArray[i].substring(lineArray[i].lastIndexOf(":") + 1,
                        lineArray[i].length());

                //add targets to mentionMap
                entities.add(new String[] { target, frequency });

                //add target to targetMap
                if (targetMap.containsKey(target)) {
                    targetMap.put(target, targetMap.get(target) + Integer.valueOf(frequency));
                } else {
                    targetMap.put(target, Integer.valueOf(frequency));
                }
            }
            mentionMap.put(lineArray[0].toLowerCase(), entities);
        }
    }
    reader.close();
}

From source file:de.tudarmstadt.ukp.dkpro.tc.crfsuite.CRFSuiteClassificationReport.java

@Override
public void execute() throws Exception {
    // only a mock for now - this needs to be rewritten anyway once the evaluation module is
    // ready/* ww w . j  a v  a 2s  . c  o  m*/
    File evalFile = new File(getContext().getStorageLocation(TEST_TASK_OUTPUT_KEY, AccessMode.READWRITE),
            CRFSuiteAdapter.getInstance().getFrameworkFilename(AdapterNameEntries.evaluationFile));

    Properties props = new Properties();
    for (String line : FileUtils.readLines(evalFile)) {
        String[] parts = line.split("=");
        props.setProperty(parts[0], parts[1]);
    }

    // Write out properties
    getContext().storeBinary(Constants.RESULTS_FILENAME, new PropertiesAdapter(props));

}

From source file:edu.cmu.lti.oaqa.annographix.util.DictNoComments.java

/**
 * Reads dictionary from a file./*from   ww w . ja va  2s .  co  m*/
 * 
 * @param file        a file object.
 * @param toLower     should we lowercase?
 * @throws Exception
 */
public DictNoComments(File file, boolean toLower) throws Exception {
    mToLower = toLower;
    for (String s : FileUtils.readLines(file)) {
        processLine(s);
    }
}

From source file:edu.isi.pfindr.learn.util.PairsFileIO.java

public static LinkedMap readDistinctElementsIntoMap(String pairsFilename) {
    File pairsFile = new File(pairsFilename);
    LinkedMap phenotypeIndexMap = new LinkedMap();
    try {//from w  w  w  .  j  av  a2  s.c o m
        List<String> fileWithPairs = FileUtils.readLines(pairsFile); //Read one at a time to consume less memory
        int index = 0;
        for (String s : fileWithPairs) {
            //distinctElementsSet.add(s.split("\t")[0]);
            //distinctElementsSet.add(s.split("\t")[1]);
            if (!phenotypeIndexMap.containsKey(s.split("\t")[0])) {
                phenotypeIndexMap.put(s.split("\t")[0], index);
                index++;
            }
        }
        for (String s : fileWithPairs) {
            if (!phenotypeIndexMap.containsKey(s.split("\t")[1])) {
                phenotypeIndexMap.put(s.split("\t")[1], index);
                index++;
            }
        }
        System.out.println("Index " + index);
    } catch (IOException e) {
        System.out.println("Error while reading/writing file with pairs" + e.getMessage());
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return phenotypeIndexMap;
}

From source file:com.netflix.turbine.discovery.FileBasedInstanceDiscovery.java

@Override
public Collection<Instance> getInstanceList() {

    List<String> lines = null;
    try {//  w  w w  .  j av  a  2 s .c  o  m
        lines = FileUtils.readLines(file);
    } catch (IOException e) {
        logger.error(
                "Error reading from file, check config property: " + filePath.getName() + "=" + filePath.get(),
                e);
    }

    List<Instance> instances = new ArrayList<Instance>();
    if (lines != null) {
        for (String line : lines) {
            try {
                instances.add(parseInstance(line));
            } catch (Exception e) {
                logger.error("Error reading from file: " + filePath.get(), e);
            }
        }
    }
    return instances;
}

From source file:ch.unibas.fittingwizard.application.tools.charges.ChargesFileParser.java

private List<ChargeValue> parseFile(File chargesFile) {
    List<String> lines;
    try {//from  w  w w  .jav a2 s  .c  o  m
        lines = FileUtils.readLines(chargesFile);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return parseLines(lines);
}

From source file:com.gargoylesoftware.htmlunit.source.SVN.java

/**
 * Ensures that all files inside the specified directory has consistent new lines.
 * @param dir the directory to recursively ensure all contained files have consistent new lines
 * @throws IOException if an exception happens
 *///from  w ww.  j av  a 2 s . com
public static void consistentNewlines(final File dir) throws IOException {
    for (final File f : dir.listFiles()) {
        if (f.isDirectory()) {
            if (!".svn".equals(f.getName())) {
                consistentNewlines(f);
            }
        } else {
            final String fileName = f.getName().toLowerCase(Locale.ROOT);
            for (final String extension : EOL_EXTENSIONS_) {
                if (fileName.endsWith(extension)) {
                    FileUtils.writeLines(f, FileUtils.readLines(f));
                    break;
                }
            }
        }
    }
}

From source file:de.tudarmstadt.ukp.similarity.experiments.coling2012.util.Features2Arff.java

@SuppressWarnings("unchecked")
private static String toArffString(Dataset dataset, Collection<File> csvFiles) throws IOException {
    // Create the Arff header
    StringBuilder arff = new StringBuilder();
    arff.append("@relation temp-relation" + LF);
    arff.append(LF);/*w  ww . j  a va 2 s  .  c  om*/

    // Init data object
    Map<Integer, List<String>> data = new HashMap<Integer, List<String>>();

    for (File file : csvFiles) {
        String feature = file.getParentFile().getName() + "/"
                + file.getName().substring(0, file.getName().length() - 4);
        // feature = feature.replaceAll(",", "");

        // Add the attribute to the Arff header
        arff.append("@attribute " + feature + " numeric" + LF);

        // Read data
        List<String> lines = FileUtils.readLines(file);
        for (int doc = 1; doc <= lines.size(); doc++) {
            String line = lines.get(doc - 1);

            if (line.length() > 0) // Ignore empty lines
            {
                Double value = Double.parseDouble(line); // There's just the score on the line, nothing else.

                // Get doc object in data list
                List<String> docObj;
                if (data.containsKey(doc))
                    docObj = data.get(doc);
                else
                    docObj = new ArrayList<String>();

                // Put data
                docObj.add(value.toString());
                data.put(doc, docObj);
            }
        }
    }

    // Read gold standard
    List<String> lines = ColingUtils.readGoldstandard(dataset);

    // Get a list of all classes (as they differ from dataset to dataset
    Set<String> allClasses = new HashSet<String>();
    for (String line : lines) {
        allClasses.add(line);
    }

    // Add gold attribute to attribute list in header
    arff.append("@attribute gold { " + StringUtils.join(allClasses, ", ") + " }" + LF);

    // Add gold similarity score 
    for (int doc = 1; doc <= lines.size(); doc++) {
        String value = lines.get(doc - 1);

        List<String> docObj = data.get(doc);
        docObj.add(value);
        data.put(doc, docObj);
    }

    // Finalize header
    arff.append(LF);
    arff.append("@data" + LF);

    // Write data
    for (int i = 1; i <= data.keySet().size(); i++) {
        String dataItem = StringUtils.join(data.get(i), ",");

        arff.append(dataItem + LF);
    }

    return arff.toString();
}

From source file:de.tudarmstadt.ukp.dkpro.tc.ml.liblinear.LiblinearClassificationReport.java

@Override
public void execute() throws Exception {
    // only a mock for now - this needs to be rewritten anyway once the evaluation module is ready
    File evalFile = new File(getContext().getStorageLocation(TEST_TASK_OUTPUT_KEY, AccessMode.READWRITE),
            LiblinearAdapter.getInstance().getFrameworkFilename(AdapterNameEntries.evaluationFile));

    Properties props = new Properties();
    for (String line : FileUtils.readLines(evalFile)) {
        String[] parts = line.split("=");
        props.setProperty(parts[0], parts[1]);
    }/*  w ww  .j  a  v a  2  s  .c o  m*/

    // Write out properties
    getContext().storeBinary(Constants.RESULTS_FILENAME, new PropertiesAdapter(props));

}