Example usage for org.apache.commons.io FileUtils lineIterator

List of usage examples for org.apache.commons.io FileUtils lineIterator

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils lineIterator.

Prototype

public static LineIterator lineIterator(File file, String encoding) throws IOException 

Source Link

Document

Returns an Iterator for the lines in a File.

Usage

From source file:net.ostis.scpdev.builder.scg.SCgFileBuilder.java

/**
 * @return true - if success, else - false.
 *//*  w ww.  j  a  va 2 s.co m*/
private boolean parseSCgFile() throws CoreException {
    IContainer parent = source.getParent();

    boolean success = true;

    try {
        LineIterator iter = FileUtils.lineIterator(new File(source.getRawLocation().toOSString()),
                source.getProject().getDefaultCharset());
        int lineNumber = 0;
        while (iter.hasNext()) {
            lineNumber++;
            String line = iter.nextLine();

            if (line.startsWith("#include")) {
                includes.add(line);
            } else {
                line = line.trim();
                if (line.endsWith(".gwf")) {
                    IFile gwfFile = parent.getFile(new Path(line));
                    if (gwfFile.exists() == false) {
                        success = false;
                        addMarker(String.format("GWF File '%s' not found ", line), lineNumber, IStatus.ERROR);
                    } else {
                        gwfs.add(gwfFile);
                    }
                }
            }
        }
    } catch (IOException e) {
        log.error("Error while parse scg file " + source, e);
        throw new CoreException(StatusUtils.makeStatus(IStatus.ERROR, e.getMessage(), e));
    }

    return success;
}

From source file:fr.ericlab.mabed.structure.Corpus.java

public void loadCorpus(boolean parallelized) {
    output = "";/* ww  w  . ja v a 2  s  .c  om*/
    if (configuration.prepareCorpus) {
        prepareCorpus();
    }
    String[] fileArray = new File("input/").list();
    nbTimeSlices = 0;
    NumberFormat formatter = new DecimalFormat("00000000");
    ArrayList<Integer> list = new ArrayList<>();
    for (String filename : fileArray) {
        if (filename.endsWith(".text")) {
            try {
                list.add(formatter.parse(filename.substring(0, 8)).intValue());
            } catch (ParseException ex) {
                Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
            }
            nbTimeSlices++;
        }
    }
    int a = Collections.min(list), b = Collections.max(list);
    distribution = new int[nbTimeSlices];
    messageCount = 0;
    LineIterator it = null;
    try {
        it = FileUtils.lineIterator(new File("input/" + formatter.format(a) + ".time"), "UTF-8");
        if (it.hasNext()) {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
            Date parsedDate = dateFormat.parse(it.nextLine());
            startTimestamp = new java.sql.Timestamp(parsedDate.getTime());
        }
        it = FileUtils.lineIterator(new File("input/" + formatter.format(b) + ".time"), "UTF-8");
        String timestamp = "";
        while (it.hasNext()) {
            timestamp = it.nextLine();
        }
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.S");
        Date parsedDate = dateFormat.parse(timestamp);
        endTimestamp = new java.sql.Timestamp(parsedDate.getTime());
    } catch (IOException | ParseException ex) {
        Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        LineIterator.closeQuietly(it);
    }
    try {
        // Global index
        FileInputStream fisMatrix = new FileInputStream("input/indexes/frequencyMatrix.dat");
        ObjectInputStream oisMatrix = new ObjectInputStream(fisMatrix);
        frequencyMatrix = (short[][]) oisMatrix.readObject();
        FileInputStream fisVocabulary = new FileInputStream("input/indexes/vocabulary.dat");
        ObjectInputStream oisVocabulary = new ObjectInputStream(fisVocabulary);
        vocabulary = (ArrayList<String>) oisVocabulary.readObject();
        // Mention index
        FileInputStream fisMentionMatrix = new FileInputStream("input/indexes/mentionFrequencyMatrix.dat");
        ObjectInputStream oisMentionMatrix = new ObjectInputStream(fisMentionMatrix);
        mentionFrequencyMatrix = (short[][]) oisMentionMatrix.readObject();
        FileInputStream fisMentionVocabulary = new FileInputStream("input/indexes/mentionVocabulary.dat");
        ObjectInputStream oisMentionVocabulary = new ObjectInputStream(fisMentionVocabulary);
        mentionVocabulary = (ArrayList<String>) oisMentionVocabulary.readObject();
        // Message count
        String messageCountStr = FileUtils.readFileToString(new File("input/indexes/messageCount.txt"));
        messageCount = Integer.parseInt(messageCountStr);
        // Message count distribution
        FileInputStream fisDistribution = new FileInputStream("input/indexes/messageCountDistribution.dat");
        ObjectInputStream oisDistribution = new ObjectInputStream(fisDistribution);
        distribution = (int[]) oisDistribution.readObject();
    } catch (FileNotFoundException ex) {
        Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException | ClassNotFoundException ex) {
        Logger.getLogger(Corpus.class.getName()).log(Level.SEVERE, null, ex);
    }
    DecimalFormat df = new DecimalFormat("#,###");
    System.out.println(Util.getDate() + " Loaded corpus:");
    output += Util.getDate() + " Loaded corpus:\n";
    info = "   - time-slices: " + df.format(nbTimeSlices) + " time-slices of " + configuration.timeSliceLength
            + " minutes each\n";
    info += "   - first message: " + startTimestamp + "\n";
    double datasetLength = (nbTimeSlices * configuration.timeSliceLength) / 60 / 24;
    info += "   - last message: " + endTimestamp + " (" + datasetLength + " days)\n";
    info += "   - number of messages: " + df.format(messageCount);
    output += info;
    System.out.println(info);
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref countries./*  w  w w.java2 s  .  com*/
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefCountries(String path) throws Exception {
    refCountries = new HashMap<String, String>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_COUNTRIES), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <commonCountryName>|akaCountryName1|akaCountryName2|...
            // For example: United States|the U.S.|the United States
            //    All three will match as a valid country, and "United States" will always be used as the common country name
            String[] countries = it.nextLine().split("[|]");
            String commonCountryName = countries[0].trim();
            refCountries.put(commonCountryName, commonCountryName);
            for (int i = 1; i < countries.length; i++)
                refCountries.put(countries[i].trim(), commonCountryName);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:itemsetmining.itemset.ItemsetTree.java

/**
 * Build the itemset-tree based on an input file containing transactions
 *
 * @param input//  w  w w . j a v  a  2 s  .  c o  m
 *            an input file
 * @return
 */
public void buildTree(final File inputFile) throws IOException {
    // record start time
    startTimestamp = System.currentTimeMillis();

    // reset memory usage statistics
    MemoryLogger.getInstance().reset();

    // create an empty root for the tree
    root = new ItemsetTreeNode(null, 0);

    // Scan the database to read the transactions
    int count = 0;
    final LineIterator it = FileUtils.lineIterator(inputFile, "UTF-8");
    while (it.hasNext()) {

        final String line = it.nextLine();
        // if the line is a comment, is empty or is a
        // kind of metadata
        if (line.isEmpty() == true || line.charAt(0) == '#' || line.charAt(0) == '%' || line.charAt(0) == '@') {
            continue;
        }

        // add transaction to the tree
        addTransaction(line);
        count++;
    }
    // close the input file
    LineIterator.closeQuietly(it);

    // set the number of transactions
    noTransactions = count;

    // check the memory usage
    MemoryLogger.getInstance().checkMemory();
    endTimestamp = System.currentTimeMillis();
}

From source file:edu.ku.brc.util.HelpIndexer.java

protected void processFile(final File file, final Vector<String> lines) {
    // System.out.println("processing file: " + file.getName());

    LineIterator it;/*from  ww w .  ja  v a 2  s.com*/
    try {
        it = FileUtils.lineIterator(file, "UTF-8");
    } catch (IOException ex) {
        edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount();
        edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(HelpIndexer.class, ex);
        System.out.println("error processing file: " + file.getName());
        return;
    }
    String target = getTarget(file);
    String title = getFileTitle(file);
    boolean removeTitleEntry = false;
    if (title != null) {
        String tline = "<indexitem text=\"" + title;
        if (target != null) {
            tline += "\"  target=\"" + target;
        }
        tline += "\">";
        lines.add(tline);
        removeTitleEntry = true;
    }
    if (target != null) {
        try {
            while (it.hasNext()) {
                String line = it.nextLine();
                //System.out.println(line);
                if (isIndexLine(line)) {
                    System.out.println("indexing " + file.getName() + ": " + line);
                    String indexEntry = processIndexLine(line, target);
                    if (indexEntry != null) {
                        lines.add("     " + indexEntry);
                        removeTitleEntry = false;
                    }
                }
            }
        } finally {
            LineIterator.closeQuietly(it);
        }
    }
    if (title != null && !removeTitleEntry) {
        lines.add("</indexitem>");
    }
    if (removeTitleEntry) {
        lines.remove(lines.size() - 1);
    }
}

From source file:com.jpetrak.gate.scala.ScalaScriptPR.java

public void tryCompileScript() {
    String scalaProgramSource;/*w w w.  jav  a 2 s . co  m*/
    String className;
    if (classloader != null) {
        Gate.getClassLoader().forgetClassLoader(classloader);
    }
    classloader = Gate.getClassLoader().getDisposableClassLoader(
            //"C"+java.util.UUID.randomUUID().toString().replaceAll("-", ""), 
            scalaProgramUrl.toExternalForm() + System.currentTimeMillis(), this.getClass().getClassLoader(),
            true);
    try {
        className = "ScalaScriptClass" + getNextId();
        StringBuilder sb = new StringBuilder();
        scalaProgramLines = new ArrayList<String>();
        scalaProgramLines.add(fileProlog);
        scalaProgramLines.add(classProlog.replaceAll("THECLASSNAME", className));
        LineIterator it = FileUtils.lineIterator(scalaProgramFile, "UTF-8");
        try {
            while (it.hasNext()) {
                String line = it.nextLine();
                scalaProgramLines.add(line);
            }
        } finally {
            LineIterator.closeQuietly(it);
        }
        scalaProgramLines.add(scalaCompiler.getClassEpilog().replaceAll("THECLASSNAME", className));
        for (String line : scalaProgramLines) {
            sb.append(line);
            sb.append("\n");
        }
        scalaProgramSource = sb.toString();

        //System.out.println("Program Source: " + scalaProgramSource);
    } catch (IOException ex) {
        System.err.println("Problem reading program from " + scalaProgramUrl);
        ex.printStackTrace(System.err);
        return;
    }
    try {
        //System.out.println("Trying to compile ...");
        scalaProgramClass = scalaCompiler.compile(className, scalaProgramSource, classloader);
        //scalaProgramClass = (ScalaScript) Gate.getClassLoader().
        //        loadClass("scalascripting." + className).newInstance();
        scalaProgramClass.globalsForPr = globalsForPr;
        scalaProgramClass.lockForPr = new Object();
        if (registeredEditorVR != null) {
            registeredEditorVR.setCompilationOk();
        }
        scalaProgramClass.resource1 = resource1;
        scalaProgramClass.resource2 = resource2;
        scalaProgramClass.resource3 = resource3;
        isCompileError = false;
        scalaProgramClass.resetInitAll();
    } catch (Exception ex) {
        System.err.println("Problem compiling ScalaScript Class");
        printScalaProgram(System.err);
        ex.printStackTrace(System.err);
        if (classloader != null) {
            Gate.getClassLoader().forgetClassLoader(classloader);
            classloader = null;
        }
        isCompileError = true;
        scalaProgramClass = null;
        if (registeredEditorVR != null) {
            registeredEditorVR.setCompilationError();
        }
        return;
    }
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref currencies countries.
 *
 * @param path the path//  w w  w . j  a  v  a2s.  c o  m
 * @throws Exception the exception
 */
private void createRefCurrenciesCountries(String path) throws Exception {
    refCountriesByCurrencies = new HashMap<String, String>();
    refCurrenciesByCountries = new HashMap<String, String>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_CURRENCIES_COUNTRIES), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <countryName>|<primaryCurrencyName>|<akaCurrencyName1>|<akaCurrencyName2>|...
            String line = it.nextLine();
            String[] fields = line.split("[|]");
            for (int i = 0; i < fields.length; i++)
                fields[i] = fields[i].trim();
            fields[1] = fields[1].trim();
            // When looking up by country, always return the primary currency
            refCurrenciesByCountries.put(fields[0], fields[1]);
            for (int i = 1; i < fields.length; i++)
                refCountriesByCurrencies.put(fields[i], fields[0]);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:eu.annocultor.converters.geonames.GeonamesCsvToRdf.java

void labels() throws Exception {
    System.out.println("Loading alt names");
    //Map<String, List<XmlValue>> altLabels = new HashMap<String, List<XmlValue>();
    LineIterator it = FileUtils.lineIterator(new File(root, "alternateNames.txt"), "UTF-8");
    try {/*from   www.ja v  a2 s  .  co  m*/
        while (it.hasNext()) {
            String text = it.nextLine();

            String[] fields = text.split("\t");
            //               if (fields.length != 4) {
            //                  throw new Exception("Field names mismatch on " + text);
            //               }
            String uri = fields[1];
            String lang = fields[2];
            String label = fields[3];
            if ("link".equals(lang)) {
                // wikipedia links
                links.put(uri, label);
            } else {
                if (lang.length() < 3) {
                    altLabels.put(uri, new LiteralValue(label, lang.isEmpty() ? null : lang));
                } else {
                    // mostly postcodes
                    //System.out.println("LANG: " + lang + " on " + label);
                }
            }
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref world regions.//from   ww w.j av a  2  s  . c o  m
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefWorldRegions(String path) throws Exception {
    refWorldRegions = new HashMap<String, Object>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_WORLD_REGIONS), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <worldRegion>
            String worldRegion = it.nextLine().trim();
            refWorldRegions.put(worldRegion, null);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:com.ipcglobal.fredimport.process.Reference.java

/**
 * Creates the ref institutions./*from   w  ww  .jav a 2 s.  co  m*/
 *
 * @param path the path
 * @throws Exception the exception
 */
private void createRefInstitutions(String path) throws Exception {
    refInstitutions = new HashMap<String, Object>();

    LineIterator it = FileUtils.lineIterator(new File(path + FILENAME_INSTITUTIONS), "UTF-8");
    try {
        while (it.hasNext()) {
            // Format: <institution>
            String institution = it.nextLine().trim();
            refWorldRegions.put(institution, null);
        }
    } finally {
        LineIterator.closeQuietly(it);
    }
}