Example usage for org.apache.commons.io.input ReversedLinesFileReader ReversedLinesFileReader

List of usage examples for org.apache.commons.io.input ReversedLinesFileReader ReversedLinesFileReader

Introduction

In this page you can find the example usage for org.apache.commons.io.input ReversedLinesFileReader ReversedLinesFileReader.

Prototype

public ReversedLinesFileReader(final File file, final int blockSize, final String encoding) throws IOException 

Source Link

Document

Creates a ReversedLinesFileReader with the given block size and encoding.

Usage

From source file:net.orzo.lib.Files.java

public FileIterator<Object> reversedFileReader(final String path, final String encoding) throws IOException {
    return new FileIterator<Object>() {

        private ReversedLinesFileReader rlf = new ReversedLinesFileReader(new File(path), 1024 * 4, encoding);
        private String currLine = rlf.readLine();

        @Override/*from ww  w .ja v  a  2 s.  c  o m*/
        public boolean hasNext() {
            return this.currLine != null;
        }

        @Override
        public Object next() {
            String ans;

            if (this.currLine != null) {
                ans = this.currLine;
                try {
                    this.currLine = this.rlf.readLine();

                } catch (IOException ex) {
                    this.currLine = null;
                    throw new IllegalStateException(ex);
                }
                return ans;

            } else {
                throw new NoSuchElementException();
            }
        }

        @Override
        public void close() {
            try {
                this.currLine = null;
                this.rlf.close();

            } catch (IOException e) {
                throw new IllegalStateException(e);
            }
        }

        @Override
        public String getPath() {
            return path;
        }
    };
}

From source file:org.springframework.shell.core.JLineShell.java

/**
 * read history commands from history log. the history size if determined by --histsize options.
 *
 * @return history commands// w  w w .j a v  a2 s .  c  o m
 */
private String[] filterLogEntry() {
    ArrayList<String> entries = new ArrayList<String>();
    try {
        ReversedLinesFileReader reader = new ReversedLinesFileReader(new File(getHistoryFileName()), 4096,
                Charset.forName("UTF-8"));
        int size = 0;
        String line = null;
        while ((line = reader.readLine()) != null) {
            if (!line.startsWith("//")) {
                size++;
                if (size > historySize) {
                    break;
                } else {
                    entries.add(line);
                }
            }
        }
    } catch (IOException e) {
        logger.warning("read history file failed. Reason:" + e.getMessage());
    }
    Collections.reverse(entries);
    return entries.toArray(new String[0]);
}