2009-03-25 23:17:38 +08:00
|
|
|
package org.broadinstitute.sting.utils;
|
|
|
|
|
|
|
|
|
|
import java.util.Iterator;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import java.util.LinkedList;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
|
|
|
|
|
/**
|
2009-03-25 23:36:21 +08:00
|
|
|
* Support for Python-like xreadlines() function as a class. This is an iterator and iterable over
|
|
|
|
|
* Strings, each corresponding a line in the file (minus newline). Enables the very simple accessing
|
|
|
|
|
* of lines in a file as:
|
|
|
|
|
*
|
|
|
|
|
* xReadLines reader = new xReadLines(new File(file_name));
|
|
|
|
|
* List<String> lines = reader.readLines();
|
|
|
|
|
* reader.close();
|
|
|
|
|
*
|
|
|
|
|
* or
|
|
|
|
|
*
|
|
|
|
|
* for ( String line : new xReadLines(new File(file_name)) {
|
|
|
|
|
* doSomeWork(line);
|
|
|
|
|
* }
|
|
|
|
|
*
|
|
|
|
|
* For the love of god, please use this system for reading lines in a file.
|
2009-03-25 23:17:38 +08:00
|
|
|
*/
|
|
|
|
|
public class xReadLines implements Iterator<String>, Iterable<String> {
|
2009-03-25 23:36:21 +08:00
|
|
|
private BufferedReader in; // The stream we're reading from
|
|
|
|
|
private String nextline = null; // Return value of next call to next()
|
|
|
|
|
private boolean trimWhitespace = true;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new xReadLines object to read lines from filename
|
|
|
|
|
*
|
|
|
|
|
* @param filename
|
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
|
*/
|
|
|
|
|
public xReadLines(final File filename, final boolean trimWhitespace) throws FileNotFoundException {
|
|
|
|
|
this(new FileReader(filename), trimWhitespace);
|
|
|
|
|
}
|
2009-03-25 23:17:38 +08:00
|
|
|
|
|
|
|
|
public xReadLines(final File filename) throws FileNotFoundException {
|
2009-03-25 23:36:21 +08:00
|
|
|
this(filename, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new xReadLines object to read lines from fileReader
|
|
|
|
|
*
|
|
|
|
|
* @param fileReader
|
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
|
*/
|
|
|
|
|
public xReadLines(final FileReader fileReader, final boolean trimWhitespace) throws FileNotFoundException {
|
|
|
|
|
this(new BufferedReader(fileReader), trimWhitespace);
|
2009-03-25 23:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public xReadLines(final FileReader fileReader) throws FileNotFoundException {
|
2009-03-25 23:36:21 +08:00
|
|
|
this(fileReader, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new xReadLines object to read lines from an input stream
|
|
|
|
|
*
|
|
|
|
|
* @param inputStream
|
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
|
*/
|
|
|
|
|
public xReadLines(final InputStream inputStream, final boolean trimWhitespace) throws FileNotFoundException {
|
|
|
|
|
this(new BufferedReader(new InputStreamReader(inputStream)), trimWhitespace);
|
2009-03-25 23:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public xReadLines(final InputStream inputStream) throws FileNotFoundException {
|
2009-03-25 23:36:21 +08:00
|
|
|
this(inputStream, true);
|
2009-03-25 23:17:38 +08:00
|
|
|
}
|
|
|
|
|
|
2009-03-25 23:36:21 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new xReadLines object to read lines from an bufferedReader
|
|
|
|
|
*
|
|
|
|
|
* @param reader
|
|
|
|
|
* @throws FileNotFoundException
|
|
|
|
|
*/
|
|
|
|
|
public xReadLines(final BufferedReader reader, final boolean trimWhitespace) throws FileNotFoundException {
|
2009-03-25 23:17:38 +08:00
|
|
|
try {
|
2009-03-25 23:36:21 +08:00
|
|
|
this.in = reader;
|
2009-03-25 23:17:38 +08:00
|
|
|
nextline = readNextLine();
|
2009-03-25 23:36:21 +08:00
|
|
|
this.trimWhitespace = trimWhitespace;
|
2009-03-25 23:17:38 +08:00
|
|
|
} catch(IOException e) {
|
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-25 23:36:21 +08:00
|
|
|
public xReadLines(final BufferedReader reader) throws FileNotFoundException {
|
|
|
|
|
this(reader, true);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reads all of the lines in the file, and returns them as a list of strings
|
|
|
|
|
*
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2009-03-25 23:17:38 +08:00
|
|
|
public List<String> readLines() {
|
|
|
|
|
List<String> lines = new LinkedList<String>();
|
|
|
|
|
for ( String line : this ) {
|
|
|
|
|
lines.add(line);
|
|
|
|
|
}
|
|
|
|
|
return lines;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-25 23:36:21 +08:00
|
|
|
/**
|
|
|
|
|
* I'm an iterator too...
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2009-03-25 23:17:38 +08:00
|
|
|
public Iterator<String> iterator() {
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public boolean hasNext() {
|
|
|
|
|
return nextline != null;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-25 23:36:21 +08:00
|
|
|
/**
|
|
|
|
|
* Actually reads the next line from the stream, not accessible publically
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2009-03-25 23:17:38 +08:00
|
|
|
private String readNextLine() throws IOException {
|
|
|
|
|
String nextline = in.readLine(); // Read another line
|
2009-03-25 23:36:21 +08:00
|
|
|
if (nextline != null && trimWhitespace )
|
2009-03-25 23:17:38 +08:00
|
|
|
nextline = nextline.trim();
|
|
|
|
|
return nextline;
|
|
|
|
|
}
|
|
|
|
|
|
2009-03-25 23:36:21 +08:00
|
|
|
/**
|
|
|
|
|
* Returns the next line (minus whitespace)
|
|
|
|
|
* @return
|
|
|
|
|
*/
|
2009-03-25 23:17:38 +08:00
|
|
|
public String next() {
|
|
|
|
|
try {
|
|
|
|
|
String result = nextline;
|
|
|
|
|
nextline = readNextLine();
|
|
|
|
|
|
|
|
|
|
// If we haven't reached EOF yet
|
|
|
|
|
if (nextline == null) {
|
|
|
|
|
in.close(); // And close on EOF
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Return the line we read last time through.
|
|
|
|
|
return result;
|
|
|
|
|
} catch(IOException e) {
|
|
|
|
|
throw new IllegalArgumentException(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// The file is read-only; we don't allow lines to be removed.
|
|
|
|
|
public void remove() {
|
|
|
|
|
throw new UnsupportedOperationException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void close() throws IOException {
|
|
|
|
|
this.in.close();
|
|
|
|
|
}
|
|
|
|
|
}
|