edu.stanford.nlp.util
Class FileLines

java.lang.Object
  extended by edu.stanford.nlp.util.FileLines
All Implemented Interfaces:
IteratorFromReaderFactory<String>, Iterable<String>, Collection<String>

public class FileLines
extends Object
implements Collection<String>, IteratorFromReaderFactory<String>

This class takes a filename and allows you to iterate through it line by line. It does _not_ read in the whole file immediately and instead reads in a new line every time the iterator is called.

Author:
Anna Rafferty, Michel Galley (gzip support)

Field Summary
static String DEFAULT_ENCODING
           
 
Constructor Summary
FileLines(String filename)
          Constructs a new FileLines object for reading lines from the given filename; checks that the file exists upon creation, but does not read until an iterator is created.
FileLines(String filename, String encoding)
          Constructs a new FileLines object for reading lines from the given filename which is in the given encoding; if encoding is not recognized, uses the default encoding for your platform
FileLines(String filename, String encoding, boolean trim)
          Constructs a new FileLines object for reading lines from the given filename which is in the given encoding; if encoding is not recognized, uses a default encoding.
FileLines(String filename, String encoding, boolean trim, Function<String,String> function)
          Constructs a new FileLines object for reading lines from the given filename which is in the given encoding; if encoding is not recognized, uses a default encoding.
 
Method Summary
 boolean add(String arg0)
          Unsupported Operation.
 boolean addAll(Collection<? extends String> c)
          Unsupported Operation.
 void clear()
          Unsupported Operation.
 boolean contains(Object o)
          Can be slow.
 boolean containsAll(Collection<?> c)
          Can be slow.
 Iterator<String> getIterator(Reader r)
           
 boolean isEmpty()
           
 Iterator<String> iterator()
           
static void main(String[] args)
           
 boolean remove(Object o)
          Unsupported Operation.
 boolean removeAll(Collection<?> c)
          Unsupported Operation.
 boolean retainAll(Collection<?> c)
          Unsupported Operation.
 int size()
          Can be slow.
 Object[] toArray()
          Can be slow.
<T> T[]
toArray(T[] a)
          Can be slow.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface java.util.Collection
equals, hashCode
 

Field Detail

DEFAULT_ENCODING

public static final String DEFAULT_ENCODING
See Also:
Constant Field Values
Constructor Detail

FileLines

public FileLines(String filename)
Constructs a new FileLines object for reading lines from the given filename; checks that the file exists upon creation, but does not read until an iterator is created.

Parameters:
filename - The file to be opened

FileLines

public FileLines(String filename,
                 String encoding)
Constructs a new FileLines object for reading lines from the given filename which is in the given encoding; if encoding is not recognized, uses the default encoding for your platform

Parameters:
filename - The file to be opened
encoding - The char encoding to be used

FileLines

public FileLines(String filename,
                 String encoding,
                 boolean trim)
Constructs a new FileLines object for reading lines from the given filename which is in the given encoding; if encoding is not recognized, uses a default encoding. If trim is true, trims excess whitespace from the lines.

Parameters:
filename -

FileLines

public FileLines(String filename,
                 String encoding,
                 boolean trim,
                 Function<String,String> function)
Constructs a new FileLines object for reading lines from the given filename which is in the given encoding; if encoding is not recognized, uses a default encoding. If trim is true, trims excess whitespace from the lines. If a function is given, the function will be applied to each line and the result will be return; function is applied after trimming if trimming is true.

Parameters:
filename -
Method Detail

iterator

public Iterator<String> iterator()
Specified by:
iterator in interface Iterable<String>
Specified by:
iterator in interface Collection<String>

add

public boolean add(String arg0)
Unsupported Operation. File-backed class

Specified by:
add in interface Collection<String>

addAll

public boolean addAll(Collection<? extends String> c)
Unsupported Operation. File-backed class

Specified by:
addAll in interface Collection<String>

clear

public void clear()
Unsupported Operation. File-backed class

Specified by:
clear in interface Collection<String>

contains

public boolean contains(Object o)
Can be slow. Usage not recommended.

Specified by:
contains in interface Collection<String>

containsAll

public boolean containsAll(Collection<?> c)
Can be slow. Usage not recommended.

Specified by:
containsAll in interface Collection<String>

isEmpty

public boolean isEmpty()
Specified by:
isEmpty in interface Collection<String>

remove

public boolean remove(Object o)
Unsupported Operation. File-backed class

Specified by:
remove in interface Collection<String>

removeAll

public boolean removeAll(Collection<?> c)
Unsupported Operation. File-backed class

Specified by:
removeAll in interface Collection<String>

retainAll

public boolean retainAll(Collection<?> c)
Unsupported Operation. File-backed class

Specified by:
retainAll in interface Collection<String>

size

public int size()
Can be slow. Usage not recommended.

Specified by:
size in interface Collection<String>

toArray

public Object[] toArray()
Can be slow. Usage not recommended.

Specified by:
toArray in interface Collection<String>

toArray

public <T> T[] toArray(T[] a)
Can be slow. Usage not recommended. String must extend T

Specified by:
toArray in interface Collection<String>

getIterator

public Iterator<String> getIterator(Reader r)
Specified by:
getIterator in interface IteratorFromReaderFactory<String>

main

public static void main(String[] args)


Stanford NLP Group