Example usage for java.io LineNumberReader LineNumberReader

List of usage examples for java.io LineNumberReader LineNumberReader

Introduction

In this page you can find the example usage for java.io LineNumberReader LineNumberReader.

Prototype

public LineNumberReader(Reader in, int sz) 

Source Link

Document

Create a new line-numbering reader, reading characters into a buffer of the given size.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    int i;//from   ww  w.j a v  a2 s .  co m
    // create new reader
    FileReader fr = new FileReader("C:/test.txt");
    LineNumberReader lnr = new LineNumberReader(fr, 200);

    // read till the end of the stream
    while ((i = lnr.read()) != -1) {
        // skip one byte
        lnr.skip(1);

        // converts integer to char
        char c = (char) i;

        // prints char
        System.out.print(c);
    }
    lnr.close();
}

From source file:org.opencms.util.CmsRfsFileViewer.java

/**
 * Return the view portion of lines of text from the underlying file or an 
 * empty String if <code>{@link #isEnabled()}</code> returns <code>false</code>.<p>
 * /*from   w  ww  .  j  a  v a2  s .c o m*/
 * @return the view portion of lines of text from the underlying file or an 
 *         empty String if <code>{@link #isEnabled()}</code> returns <code>false</code>
 * @throws CmsRfsException if something goes wrong
 */
public String readFilePortion() throws CmsRfsException {

    if (m_enabled) {
        // if we want to view the log file we have to set the internal m_windowPos to the last window 
        // to view the end: 
        int lines = -1;
        int startLine;
        if (m_isLogfile) {
            lines = scrollToFileEnd();
            // for logfile mode we show the last window of window size: 
            // it could be possible that only 4 lines are in the last window 
            // (e.g.: 123 lines with windowsize 10 -> last window has 3 lines) 
            // so we ignore the window semantics and show the n last lines: 
            startLine = lines - m_windowSize;
        } else {
            m_windowPos = 0;
            startLine = m_windowPos * m_windowSize;
        }
        LineNumberReader reader = null;
        try {
            // don't make the buffer too big, just big enough for windowSize lines (estimation: avg. of 200 characters per line) 
            // to save reading too much (this optimizes to read the first windows, much later windows will be slower...)
            reader = new LineNumberReader(
                    new BufferedReader(new InputStreamReader(new FileInputStream(m_filePath), m_fileEncoding)),
                    m_windowSize * 200);
            int currentLine = 0;
            // skip the lines to the current window:
            while (startLine > currentLine) {
                reader.readLine();
                currentLine++;
            }
            StringBuffer result = new StringBuffer();
            String read = reader.readLine();

            // logfile treatment is different
            // we invert the lines: latest come first
            if (m_isLogfile) {
                // stack is java hall of shame member... but standard
                Stack inverter = new Stack();
                for (int i = m_windowSize; (i > 0) && (read != null); i--) {
                    inverter.push(read);
                    read = reader.readLine();
                }
                // pop-off:
                while (!inverter.isEmpty()) {
                    result.append(inverter.pop());
                    result.append('\n');
                }
            } else {
                for (int i = m_windowSize; (i > 0) && (read != null); i--) {
                    result.append(read);
                    result.append('\n');
                    read = reader.readLine();
                }
            }
            return CmsEncoder.escapeXml(result.toString());
        } catch (IOException ioex) {
            CmsRfsException ex = new CmsRfsException(
                    Messages.get().container(Messages.ERR_FILE_ARG_ACCESS_1, m_filePath), ioex);
            throw ex;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    LOG.error(e.getLocalizedMessage(), e);
                }
            }
        }
    } else {
        return Messages.get().getBundle().key(Messages.GUI_FILE_VIEW_NO_PREVIEW_0);
    }
}