Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package divconq.io;
 
 import java.io.Reader;
 
An Iterator over the lines in a Reader.

LineIterator holds a reference to an open Reader. When you have finished with the iterator you should close the reader to free internal resources. This can be done by closing the reader directly, or by calling the close() or closeQuietly(divconq.io.LineIterator) method on the iterator.

The recommended usage pattern is:

 LineIterator it = FileUtils.lineIterator(file, "UTF-8");
 try {
   while (it.hasNext()) {
     String line = it.nextLine();
     // do something with line
   }
 } finally {
   it.close();
 }
 

Version:
$Id$
Since:
1.2
 
 public class LineIterator implements Iterator<String> {
 
     // N.B. This class deliberately does not implement Iterable, see https://issues.apache.org/jira/browse/IO-181
 
    
The reader that is being read.
 
     private final BufferedReader bufferedReader;
    
The current line.
 
     private String cachedLine;
    
A flag indicating if the iterator has been fully read.
 
     private boolean finished = false;

    
Constructs an iterator of the lines for a Reader.

Parameters:
reader the Reader to read from, not null
Throws:
java.lang.IllegalArgumentException if the reader is null
 
     public LineIterator(final Reader readerthrows IllegalArgumentException {
         if (reader == null) {
             throw new IllegalArgumentException("Reader must not be null");
         }
         if (reader instanceof BufferedReader) {
              = (BufferedReaderreader;
         } else {
              = new BufferedReader(reader);
         }
     }
 
     //-----------------------------------------------------------------------
     
Indicates whether the Reader has more lines. If there is an IOException then close() will be called on this instance.

Returns:
true if the Reader has more lines
Throws:
java.lang.IllegalStateException if an IO exception occurs
 
     public boolean hasNext() {
         if ( != null) {
             return true;
         } else if () {
             return false;
         } else {
             try {
                 while (true) {
                     final String line = .readLine();
                     if (line == null) {
                          = true;
                        return false;
                    } else if (isValidLine(line)) {
                         = line;
                        return true;
                    }
                }
            } catch(final IOException ioe) {
                close();
                throw new IllegalStateException(ioe);
            }
        }
    }

    
Overridable method to validate each line that is returned. This implementation always returns true.

Parameters:
line the line that is to be validated
Returns:
true if valid, false to remove from the iterator
    protected boolean isValidLine(final String line) {
        return true;
    }

    
Returns the next line in the wrapped Reader.

Returns:
the next line from the input
Throws:
java.util.NoSuchElementException if there is no line to return
    public String next() {
        return nextLine();
    }

    
Returns the next line in the wrapped Reader.

Returns:
the next line from the input
Throws:
java.util.NoSuchElementException if there is no line to return
    public String nextLine() {
        if (!hasNext()) {
            throw new NoSuchElementException("No more lines");
        }
        final String currentLine = ;
         = null;
        return currentLine;
    }

    
Closes the underlying Reader quietly. This method is useful if you only want to process the first few lines of a larger file. If you do not close the iterator then the Reader remains open. This method can safely be called multiple times.
    public void close() {
         = true;
        IOUtil.closeQuietly();
         = null;
    }

    
Unsupported.

    public void remove() {
        throw new UnsupportedOperationException("Remove unsupported on LineIterator");
    }
    //-----------------------------------------------------------------------
    
Closes the iterator, handling null and ignoring exceptions.

Parameters:
iterator the iterator to close
    public static void closeQuietly(final LineIterator iterator) {
        if (iterator != null) {
            iterator.close();
        }
    }
New to GrepCode? Check out our FAQ X