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:org.freeeed.main.FileProcessor.java

private void extractJlFields(DiscoveryFile discoveryFile) {
    LineIterator it = null;//from w w  w  .j a v a 2s  .co m
    try {
        it = FileUtils.lineIterator(discoveryFile.getPath(), "UTF-8");
        while (it.hasNext()) {
            DocumentMetadata metadata = new DocumentMetadata();
            String jsonAsString = it.nextLine();
            String htmlText = JsonParser.getJsonField(jsonAsString, "extracted_text");
            String text = Jsoup.parse(htmlText).text();
            // text metadata fields
            metadata.set(DocumentMetadataKeys.DOCUMENT_TEXT, text);
            metadata.setContentType("application/jl");
            // other necessary metadata fields
            metadata.setOriginalPath(getOriginalDocumentPath(discoveryFile));
            metadata.setHasAttachments(discoveryFile.isHasAttachments());
            metadata.setHasParent(discoveryFile.isHasParent());
            metadata.setCustodian(Project.getCurrentProject().getCurrentCustodian());
            writeMetadata(discoveryFile, metadata);
            ESIndex.getInstance().addBatchData(metadata);
        }
    } catch (Exception e) {
        LOGGER.error("Problem with JSON line", e);
    } finally {
        if (it != null) {
            it.close();
        }
    }
}

From source file:org.h819.commons.file.MyFileUtils.java

/**
 * ????????/*from  w w w .jav  a2  s  . co m*/
 */
private static void exampleReadLargeFile() {

    File theFile = new File("");
    LineIterator it = null;

    try {

        it = FileUtils.lineIterator(theFile, "UTF-8");
        while (it.hasNext()) {
            String line = it.nextLine();
            // do something with line
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:org.kalypso.wspwin.core.CalculationBean.java

public static CalculationBean[] readBerFile(final File berFile) throws ParseException, IOException {
    // if a zustand has no calculations, no .ber file is present.
    if (!berFile.exists())
        return new CalculationBean[0];

    final List<CalculationBean> beans = new ArrayList<>(10);

    LineIterator lineIt = null;//  w  ww.ja va  2s.  c  o  m
    try {
        int count = 0;
        lineIt = FileUtils.lineIterator(berFile, "CP850"); //$NON-NLS-1$

        // ignore first line, we just read all lines
        lineIt.nextLine();
        count++;

        while (lineIt.hasNext()) {
            final String line = lineIt.nextLine();
            count++;

            if (line.length() < 60)
                throw new ParseException(Messages.getString("org.kalypso.wspwin.core.CalculationBean.0") + line, //$NON-NLS-1$
                        count);

            final String name = line.substring(0, 60).trim();

            final StringTokenizer tokenizer = new StringTokenizer(line.substring(60));

            if (tokenizer.countTokens() != 3)
                throw new ParseException(Messages.getString("org.kalypso.wspwin.core.CalculationBean.1") + line, //$NON-NLS-1$
                        count);

            final BigDecimal fromStation = new BigDecimal(tokenizer.nextToken());
            final BigDecimal toStation = new BigDecimal(tokenizer.nextToken());
            final String fileName = tokenizer.nextToken();

            beans.add(new CalculationBean(name, fileName, fromStation, toStation));
        }

        return beans.toArray(new CalculationBean[beans.size()]);
    } finally {
        LineIterator.closeQuietly(lineIt);
    }
}

From source file:org.kalypso.wspwin.core.LocalEnergyLossBean.java

/**
 * Reads a psi file (Energieverluste/Verlustbeiwerte/local energy losses)
 *///w w w  . j  a  v  a2  s  . c om
public static LocalEnergyLossBean[] read(final File lelFile) throws ParseException, IOException {
    final List<LocalEnergyLossBean> beans = new ArrayList<>(0);

    LineIterator lineIt = null;
    try {
        if (lelFile.exists()) {
            int count = 0;
            for (lineIt = FileUtils.lineIterator(lelFile, null); lineIt.hasNext();) {
                final String nextLine = lineIt.nextLine();
                count++;

                final StringTokenizer tokenizer = new StringTokenizer(nextLine);
                if (tokenizer.countTokens() % 2 != 0)
                    throw new ParseException(
                            Messages.getString("org.kalypso.wspwin.core.LocalEnergyLossBean.1") + nextLine, //$NON-NLS-1$
                            count);

                final int countKinds = tokenizer.countTokens() / 2 - 1;

                final String key = tokenizer.nextToken();

                if (!STATION.equalsIgnoreCase(key))
                    throw new ParseException(Messages.getString("org.kalypso.wspwin.core.LocalEnergyLossBean.2") //$NON-NLS-1$
                            + STATION + "': " + nextLine, count); //$NON-NLS-1$

                final BigDecimal station = new BigDecimal(tokenizer.nextToken());

                // read pairs: kind -> value
                final Collection<Pair<String, BigDecimal>> entries = new ArrayList<>();
                for (int i = 0; i < countKinds; i++) {
                    final String kind = tokenizer.nextToken();
                    final BigDecimal value = new BigDecimal(tokenizer.nextToken());
                    entries.add(Pair.of(kind, value));
                }

                final LocalEnergyLossBean lossBean = new LocalEnergyLossBean(station,
                        entries.toArray(new Pair[entries.size()]));
                beans.add(lossBean);
            }
        }
        return beans.toArray(new LocalEnergyLossBean[beans.size()]);
    } finally {
        LineIterator.closeQuietly(lineIt);
    }
}

From source file:org.kalypso.wspwin.core.RunOffEventBean.java

/** Reads a qwt or wsf file */
public static RunOffEventBean[] read(final File qwtFile) throws ParseException, IOException {
    // the qwt and wsf files may not exist; return empty list of beans
    if (!qwtFile.exists())
        return new RunOffEventBean[] {};

    final List<RunOffEventBean> beans = new ArrayList<>(10);

    LineIterator lineIt = null;/*www  .  j  av a 2 s. c o  m*/
    try {
        int count = 0;
        for (lineIt = FileUtils.lineIterator(qwtFile, null); lineIt.hasNext();) {
            final String nextLine = lineIt.nextLine().trim();
            count++;

            if (nextLine.isEmpty())
                continue;

            final StringTokenizer tokenizer = new StringTokenizer(nextLine);
            if (tokenizer.countTokens() != 2)
                throw new ParseException(
                        Messages.getString("org.kalypso.wspwin.core.RunOffEventBean.0") + nextLine, count); //$NON-NLS-1$

            final String eventName = tokenizer.nextToken();

            final RunOffEventBean bean = new RunOffEventBean(eventName);

            final int eventLength = Integer.parseInt(tokenizer.nextToken());

            // read block: station -> value
            for (int i = 0; i < eventLength; i++) {
                if (!lineIt.hasNext())
                    throw new ParseException(
                            Messages.getString("org.kalypso.wspwin.core.RunOffEventBean.1") + eventName, count); //$NON-NLS-1$

                final String line = lineIt.nextLine();
                count++;
                final StringTokenizer tz = new StringTokenizer(line);
                if (tz.countTokens() != 2)
                    throw new ParseException(
                            Messages.getString("org.kalypso.wspwin.core.RunOffEventBean.2") + nextLine, count); //$NON-NLS-1$

                final double station = Double.parseDouble(tz.nextToken());
                final double value = Double.parseDouble(tz.nextToken());
                bean.addEntry(BigDecimal.valueOf(station), BigDecimal.valueOf(value));
            }

            beans.add(bean);
        }

        return beans.toArray(new RunOffEventBean[beans.size()]);
    } finally {
        LineIterator.closeQuietly(lineIt);
    }
}

From source file:org.klab.com.etl.Movies.java

public void readFile(String file, BufferedWriter bw) {
    int _n_ = 0;//from  w w  w.  ja v  a2 s .  c o  m
    int _r_ = 0;
    LineIterator it = null;
    String FILE_DATE = movieFileDate(file);
    try {
        it = FileUtils.lineIterator(new File(file), "UTF-8");
        while (it.hasNext()) {
            String line = it.nextLine();

            int amountOfTabsLine = StringUtils.countMatches(line, SEPARATOR);
            //This will decide the structure
            String fixedLine = fixLine(line);
            // do something with line
            // System.out.println("N:\t"+amountOfTabsLine+ "\t"+line);
            if (!fixedLine.isEmpty() && _n_ > 0) {
                String output = _n_ + "\t" + FILE_DATE + "\t" + amountOfTabsLine + "\t" + fixedLine;
                bw.write(output);
                bw.newLine();
                _r_++;
            }

            _n_++;
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } finally {
        System.out.println("io > [" + _n_ + "][" + _r_ + "] > out");
        LineIterator.closeQuietly(it);
    }
}

From source file:org.klab.com.etl.Movies.java

public void readinHeader(File file) {
    LineIterator it = null;/*from  w w w  .  ja v  a 2  s. c om*/
    try {
        it = FileUtils.lineIterator(file, "UTF-8");
        if (it.hasNext()) {
            String line = it.nextLine();
            getHeaderStructure(line);
        }
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:org.mskcc.cbio.portal.util.IGVLinking.java

private static String getFileContents(File file) throws Exception {
    StringBuilder sb = new StringBuilder();
    LineIterator it = null;//  ww  w  .  jav  a 2  s .  c o m

    try {
        it = FileUtils.lineIterator(file, "UTF-8");
        while (it.hasNext()) {
            sb.append(it.nextLine());
        }
    } finally {
        if (it != null)
            it.close();
    }

    return sb.toString();
}

From source file:org.nomt.agent.nmon.job.ParserJob.java

@Override
public void perform() {
    logger.info("ParserJob.perform()");
    String filename = NmonDataFiles.getNextFile();
    File nmonDataFile = new File(filename);
    logger.debug("Found nmon file {}, start to parser it.", filename);
    try {/*w ww  .j  a  v a  2  s.c  o m*/
        LineIterator lineIterator = FileUtils.lineIterator(nmonDataFile, "utf-8");
        Aaa aaa = new Aaa();
        CpuX86 cpuX86 = new CpuX86();
        CpuAll cpuAll = new CpuAll();
        Mem mem = new Mem();
        Net net = new Net();
        NetPacket netPacket = new NetPacket();
        DiskBusy diskBusy = new DiskBusy();
        DiskBsize diskBsize = new DiskBsize();
        DiskRead diskRead = new DiskRead();
        DiskWrite diskWrite = new DiskWrite();
        DiskXfer diskXfer = new DiskXfer();
        JfsInode jfsInode = new JfsInode();
        Proc proc = new Proc();

        String nextLine;
        String[] lineParts;
        String part0, part1;
        String[] netTitleLineParts = null;
        String[] netPacketTitleLineParts = null;
        String[] jfsLineParts = null;
        String[] diskBusyLineParts = null;
        String[] diskReadLineParts = null;
        String[] diskWriteLineParts = null;
        String[] diskBsizeLineParts = null;
        String[] diskXferLineParts = null;

        while (lineIterator.hasNext()) {
            nextLine = lineIterator.next();
            lineParts = nextLine.split(",");
            part0 = lineParts[0];
            part1 = lineParts[1];
            if ("AAA".equalsIgnoreCase(part0)) {
                if (lineParts.length == 3) {
                    NmonLineParser.parseAaa(aaa, lineParts);
                } else if (lineParts.length == 4) {
                    NmonLineParser.parseCpuX86(cpuX86, lineParts);
                }
            } else if ("T0001".equalsIgnoreCase(part1)) {
                if ("CPU_ALL".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseCpuAll(cpuAll, lineParts);
                } else if ("MEM".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseMem(mem, lineParts);
                } else if ("NET".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseNet(net, netTitleLineParts, lineParts);
                } else if ("NETPACKET".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseNetPacket(netPacket, netPacketTitleLineParts, lineParts);
                } else if ("JFSFILE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseJfsInode(jfsInode, jfsLineParts, lineParts);
                } else if ("DISKBUSY".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskBusy(diskBusy, diskBusyLineParts, lineParts);
                } else if ("DISKREAD".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskRead(diskRead, diskReadLineParts, lineParts);
                } else if ("DISKWRITE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskWrite(diskWrite, diskWriteLineParts, lineParts);
                } else if ("DISKXFER".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskXfer(diskXfer, diskXferLineParts, lineParts);
                } else if ("DISKBSIZE".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseDiskBsize(diskBsize, diskBsizeLineParts, lineParts);
                } else if ("PROC".equalsIgnoreCase(part0)) {
                    NmonLineParser.parseProc(proc, lineParts);
                }
            } else if ("NET".equalsIgnoreCase(part0)) {
                netTitleLineParts = lineParts;
            } else if ("NETPACKET".equalsIgnoreCase(part0)) {
                netPacketTitleLineParts = lineParts;
            } else if ("JFSFILE".equalsIgnoreCase(part0)) {
                jfsLineParts = lineParts;
            } else if ("DISKBUSY".equalsIgnoreCase(part0)) {
                diskBusyLineParts = lineParts;
            } else if ("DISKREAD".equalsIgnoreCase(part0)) {
                diskReadLineParts = lineParts;
            } else if ("DISKWRITE".equalsIgnoreCase(part0)) {
                diskWriteLineParts = lineParts;
            } else if ("DISKXFER".equalsIgnoreCase(part0)) {
                diskXferLineParts = lineParts;
            } else if ("DISKBSIZE".equalsIgnoreCase(part0)) {
                diskBsizeLineParts = lineParts;
            }

        }

        NmonFileInfo nmonFileInfo = new NmonFileInfo();
        nmonFileInfo.setAaa(aaa);
        nmonFileInfo.setX86(cpuX86);
        nmonFileInfo.setCpuAll(cpuAll);
        nmonFileInfo.setNet(net);
        nmonFileInfo.setNetPacket(netPacket);
        nmonFileInfo.setMem(mem);
        nmonFileInfo.setJfsInode(jfsInode);
        nmonFileInfo.setDiskBusy(diskBusy);
        nmonFileInfo.setDiskRead(diskRead);
        nmonFileInfo.setDiskWrite(diskWrite);
        nmonFileInfo.setDiskXfer(diskXfer);
        nmonFileInfo.setBsize(diskBsize);
        NmonFileInfoRepo.setContent(nmonFileInfo);
        logger.debug("Parse Nmon File {} successfully.", filename);
    } catch (IOException e) {
        logger.info("IOException while parsering {}.", filename);
    }

    FileUtils.deleteQuietly(nmonDataFile);
    logger.debug("After parsering, delete file {}.", filename);
}

From source file:org.objectstyle.woproject.maven2.wobootstrap.utils.WebObjectsUtils.java

/**
 * Retrieves the version of installed WebObjects. It uses a
 * <code>WebObjectsLocator</code> to find the WebObjects version file.
 * /* w  w  w  . jav  a 2  s. co  m*/
 * @param locator
 *            The WebObjects locator
 * @return Returns the WebObjects version or <code>null</code> if cannot
 *         discover the WebObjects version
 */
public static String getWebObjectsVersion(WebObjectsLocator locator) {
    if (locator == null) {
        return null;
    }

    File versionFile = locator.getWebObjectsVersionFile();

    if (versionFile == null || !versionFile.exists()) {
        return null;
    }

    String version = null;

    LineIterator iterator = null;

    try {
        iterator = FileUtils.lineIterator(versionFile, null);

        while (iterator.hasNext()) {
            String line = iterator.nextLine();

            if ("<key>CFBundleShortVersionString</key>".equals(line.trim())) {
                String versionLine = iterator.nextLine();

                version = versionLine.trim().replaceAll("</?string>", "");

                break;
            }
        }

    } catch (IOException exception) {
        // TODO: hprange, write an info to log instead
        exception.printStackTrace();
    } finally {
        if (iterator != null) {
            LineIterator.closeQuietly(iterator);
        }
    }

    return version;
}