javatools.filehandlers
Class FileLines

java.lang.Object
  extended by javatools.datatypes.PeekIterator<java.lang.String>
      extended by javatools.filehandlers.FileLines
All Implemented Interfaces:
java.io.Closeable, java.lang.Iterable<java.lang.String>, java.util.Iterator<java.lang.String>
Direct Known Subclasses:
ByteFileLines

public class FileLines
extends PeekIterator<java.lang.String>
implements java.lang.Iterable<java.lang.String>, java.util.Iterator<java.lang.String>, java.io.Closeable

This class is part of the Java Tools (see http://mpii.de/yago-naga/javatools). It is licensed under the Creative Commons Attribution License (see http://creativecommons.org/licenses/by/3.0) by the YAGO-NAGA team (see http://mpii.de/yago-naga). The class provides an iterator over the lines in a file
Example:

 for (String s : new FileLines("c:\\autoexec.bat")) {
        System.out.println(s);
 }
 
If desired, the iterator can make a nice progress bar by calling Announce.progressStart/At/Done automatically in the right order. If there are no more lines, the file is closed. If you do not use all lines of the iterator, close the iterator manually.


Nested Class Summary
 
Nested classes/interfaces inherited from class javatools.datatypes.PeekIterator
PeekIterator.ElementaryPeekIterator<T>, PeekIterator.SimplePeekIterator<T>
 
Field Summary
static long maxChars
          Maximum chars to read by readTo (or -1)
 
Fields inherited from class javatools.datatypes.PeekIterator
closed, initialized, next
 
Constructor Summary
FileLines(java.io.BufferedReader r)
          Constructs FileLines from a BufferedReader (main constructor 3)
FileLines(java.io.File f)
          Constructs FileLines from a file
FileLines(java.io.File f, java.lang.String announceMsg)
          Constructs FileLines from a file, shows progress bar (main constructor 2)
FileLines(java.io.File f, java.lang.String encoding, java.lang.String announceMsg)
          Constructs FileLines from a file with an encoding, shows progress bar (main constructor 1)
FileLines(java.io.Reader f)
          Constructs FileLines from a Reader
FileLines(java.lang.String f)
          Constructs FileLines from a filename
FileLines(java.lang.String f, java.lang.String announceMsg)
          Constructs FileLines from a filename, shows progress bar
FileLines(java.lang.String f, java.lang.String encoding, java.lang.String announceMsg)
          Constructs FileLines from a filename with a given encoding, shows progress bar
 
Method Summary
 void close()
          Closes the reader
 void finalize()
          Closes the reader
static int find(java.io.Reader in, java.lang.String... findMe)
          Reads until one of the strings is found, returns its index or -1.
static int find(java.io.Reader in, java.lang.StringBuilder b, java.lang.String... findMe)
          Reads until one of the strings is found, returns its index or -1.
static int findIgnoreCase(java.io.Reader in, java.lang.String... findMe)
          Reads until one of the strings is found, returns its index or -1.
static int firstCharAfterSpace(java.io.Reader in)
          Skips space
 java.lang.String internalNext()
          Returns next line.
 java.util.Iterator<java.lang.String> iterator()
          Returns this
static int numAllFileLines(java.io.File f, java.lang.String ext)
          returns number of lines in file
static int numFileLines(java.io.File f)
          returns number of lines in file
static java.lang.String readBetween(java.io.Reader in, java.lang.String start, java.lang.String end)
          Reads the string between the delimiters
static java.lang.String readBetween(java.lang.String in, java.lang.String start, java.lang.String end)
          Reads the string between the delimiters
static java.lang.CharSequence readTo(java.io.Reader in, char... limit)
          Reads to a specific character, returns the text in between
static java.lang.CharSequence readTo(java.io.Reader in, char limit)
          Reads to a specific character, returns the text in between
static java.lang.CharSequence readTo(java.io.Reader in, java.lang.String limit)
          Reads to a specific String, returns the text up to there, including the limit
static java.lang.CharSequence readTo(java.io.Reader in, java.lang.String limit, java.util.List<java.lang.Integer> results)
          Reads to a specific String, returns the text up to there, including the limit
static java.lang.String readToBoundary(java.io.Reader in, java.lang.String limit)
          Reads to a specific string, returns text up to there, without boundary or returns NULL
static java.lang.CharSequence readToSpace(java.io.Reader in)
          Reads to a whitespace
 void remove()
          Unsupported, throws an UnsupportedOperationException
static boolean scrollTo(java.io.Reader in, char... delimiters)
          Scrolls to one of the characters
static boolean scrollTo(java.io.Reader in, char delimiter)
          Reads to a specific character
static boolean scrollTo(java.io.Reader in, java.lang.String limit)
          Scrolls to a specific limit and beyond
 java.lang.String toString()
          Returns a simple identifier
 
Methods inherited from class javatools.datatypes.PeekIterator
asList, asList, asSet, asSet, asSet, emptyIterator, hasNext, list, list, main, next, nextOrNull, numElements, numElements, peek, toString, toString
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Iterator
hasNext, next
 

Field Detail

maxChars

public static long maxChars
Maximum chars to read by readTo (or -1)

Constructor Detail

FileLines

public FileLines(java.lang.String f)
          throws java.io.IOException
Constructs FileLines from a filename

Throws:
java.io.IOException

FileLines

public FileLines(java.io.File f)
          throws java.io.IOException
Constructs FileLines from a file

Throws:
java.io.IOException

FileLines

public FileLines(java.lang.String f,
                 java.lang.String announceMsg)
          throws java.io.IOException
Constructs FileLines from a filename, shows progress bar

Throws:
java.io.IOException

FileLines

public FileLines(java.lang.String f,
                 java.lang.String encoding,
                 java.lang.String announceMsg)
          throws java.io.IOException
Constructs FileLines from a filename with a given encoding, shows progress bar

Throws:
java.io.IOException

FileLines

public FileLines(java.io.File f,
                 java.lang.String encoding,
                 java.lang.String announceMsg)
          throws java.io.IOException
Constructs FileLines from a file with an encoding, shows progress bar (main constructor 1)

Throws:
java.io.IOException

FileLines

public FileLines(java.io.File f,
                 java.lang.String announceMsg)
          throws java.io.IOException
Constructs FileLines from a file, shows progress bar (main constructor 2)

Throws:
java.io.IOException

FileLines

public FileLines(java.io.Reader f)
Constructs FileLines from a Reader


FileLines

public FileLines(java.io.BufferedReader r)
Constructs FileLines from a BufferedReader (main constructor 3)

Method Detail

remove

public void remove()
            throws java.lang.UnsupportedOperationException
Unsupported, throws an UnsupportedOperationException

Specified by:
remove in interface java.util.Iterator<java.lang.String>
Overrides:
remove in class PeekIterator<java.lang.String>
Throws:
java.lang.UnsupportedOperationException

internalNext

public java.lang.String internalNext()
Returns next line. In case of an IOException, the exception is wrapped in a RuntimeException


toString

public java.lang.String toString()
Returns a simple identifier

Overrides:
toString in class PeekIterator<java.lang.String>

iterator

public java.util.Iterator<java.lang.String> iterator()
Returns this

Specified by:
iterator in interface java.lang.Iterable<java.lang.String>
Overrides:
iterator in class PeekIterator<java.lang.String>

close

public void close()
Closes the reader

Specified by:
close in interface java.io.Closeable
Overrides:
close in class PeekIterator<java.lang.String>

finalize

public void finalize()
Closes the reader

Overrides:
finalize in class java.lang.Object

find

public static int find(java.io.Reader in,
                       java.lang.String... findMe)
                throws java.io.IOException
Reads until one of the strings is found, returns its index or -1.

Throws:
java.io.IOException

find

public static int find(java.io.Reader in,
                       java.lang.StringBuilder b,
                       java.lang.String... findMe)
                throws java.io.IOException
Reads until one of the strings is found, returns its index or -1.

Throws:
java.io.IOException

findIgnoreCase

public static int findIgnoreCase(java.io.Reader in,
                                 java.lang.String... findMe)
                          throws java.io.IOException
Reads until one of the strings is found, returns its index or -1.

Throws:
java.io.IOException

readTo

public static java.lang.CharSequence readTo(java.io.Reader in,
                                            char... limit)
                                     throws java.io.IOException
Reads to a specific character, returns the text in between

Throws:
java.io.IOException

readToSpace

public static java.lang.CharSequence readToSpace(java.io.Reader in)
                                          throws java.io.IOException
Reads to a whitespace

Throws:
java.io.IOException

firstCharAfterSpace

public static int firstCharAfterSpace(java.io.Reader in)
                               throws java.io.IOException
Skips space

Throws:
java.io.IOException

readTo

public static java.lang.CharSequence readTo(java.io.Reader in,
                                            char limit)
                                     throws java.io.IOException
Reads to a specific character, returns the text in between

Throws:
java.io.IOException

readTo

public static java.lang.CharSequence readTo(java.io.Reader in,
                                            java.lang.String limit)
                                     throws java.io.IOException
Reads to a specific String, returns the text up to there, including the limit

Throws:
java.io.IOException

readToBoundary

public static java.lang.String readToBoundary(java.io.Reader in,
                                              java.lang.String limit)
                                       throws java.io.IOException
Reads to a specific string, returns text up to there, without boundary or returns NULL

Throws:
java.io.IOException

scrollTo

public static boolean scrollTo(java.io.Reader in,
                               char... delimiters)
                        throws java.io.IOException
Scrolls to one of the characters

Throws:
java.io.IOException

scrollTo

public static boolean scrollTo(java.io.Reader in,
                               char delimiter)
                        throws java.io.IOException
Reads to a specific character

Throws:
java.io.IOException

scrollTo

public static boolean scrollTo(java.io.Reader in,
                               java.lang.String limit)
                        throws java.io.IOException
Scrolls to a specific limit and beyond

Throws:
java.io.IOException

readBetween

public static java.lang.String readBetween(java.io.Reader in,
                                           java.lang.String start,
                                           java.lang.String end)
                                    throws java.io.IOException
Reads the string between the delimiters

Throws:
java.io.IOException

readBetween

public static java.lang.String readBetween(java.lang.String in,
                                           java.lang.String start,
                                           java.lang.String end)
Reads the string between the delimiters

Throws:
java.io.IOException

readTo

public static java.lang.CharSequence readTo(java.io.Reader in,
                                            java.lang.String limit,
                                            java.util.List<java.lang.Integer> results)
                                     throws java.io.IOException
Reads to a specific String, returns the text up to there, including the limit

Throws:
java.io.IOException

numFileLines

public static int numFileLines(java.io.File f)
                        throws java.io.IOException
returns number of lines in file

Throws:
java.io.IOException

numAllFileLines

public static int numAllFileLines(java.io.File f,
                                  java.lang.String ext)
                           throws java.io.IOException
returns number of lines in file

Throws:
java.io.IOException