public class EntryReader
extends java.io.LineNumberReader
implements java.lang.Iterable<java.lang.String>, java.util.Iterator<java.lang.String>
Example use:
// EntryReader constructor args are: filename, comment regexp, include regexp
try (EntryReader er = new EntryReader(filename, "^#.*", null)) {
for (String line : er) {
...
}
} catch (IOException e) {
System.err.println("Problem reading " + filename + ": " + e.getMessage());
}
| Modifier and Type | Class and Description |
|---|---|
static class |
EntryReader.Entry
Descriptor for an entry (record, paragraph, etc.).
|
| Modifier and Type | Field and Description |
|---|---|
@MonotonicNonNull java.util.regex.Pattern |
entryStartRegex
Regular expression that starts a long entry.
|
@MonotonicNonNull java.util.regex.Pattern |
entryStopRegex |
| Constructor and Description |
|---|
EntryReader(java.io.File file)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.io.File file,
java.lang.String charsetName)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.io.File file,
@Nullable java.lang.String commentRegex,
@Nullable java.lang.String includeRegex)
Create an EntryReader.
|
EntryReader(java.io.InputStream in)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.io.InputStream in,
java.lang.String filename)
Create a EntryReader that uses the default character set and does not support comments or
include directives.
|
EntryReader(java.io.InputStream in,
java.lang.String charsetName,
java.lang.String filename)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.io.InputStream in,
java.lang.String filename,
@Nullable java.lang.String commentRegexString,
@Nullable java.lang.String includeRegexString)
Create a EntryReader.
|
EntryReader(java.io.InputStream in,
java.lang.String charsetName,
java.lang.String filename,
@Nullable java.lang.String commentRegexString,
@Nullable java.lang.String includeRegexString)
Create a EntryReader that uses the given character set.
|
EntryReader(java.nio.file.Path path)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.nio.file.Path path,
java.lang.String charsetName)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.nio.file.Path path,
@Nullable java.lang.String commentRegex,
@Nullable java.lang.String includeRegex)
Create an EntryReader.
|
EntryReader(java.io.Reader reader)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.io.Reader reader,
java.lang.String filename,
@Nullable java.lang.String commentRegexString,
@Nullable java.lang.String includeRegexString)
Create a EntryReader.
|
EntryReader(java.lang.String filename)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.lang.String filename,
java.lang.String charsetName)
Create a EntryReader that does not support comments or include directives.
|
EntryReader(java.lang.String filename,
@Nullable java.lang.String commentRegex,
@Nullable java.lang.String includeRegex)
Create a new EntryReader starting with the specified file.
|
| Modifier and Type | Method and Description |
|---|---|
@Nullable EntryReader.Entry |
getEntry()
Returns the next entry (paragraph) in the file.
|
java.lang.String |
getFileName()
Returns the current filename.
|
int |
getLineNumber()
Return the current line number in the current file.
|
boolean |
hasNext()
Returns whether or not there is another line to read.
|
java.util.Iterator<java.lang.String> |
iterator()
Return a line-by-line iterator for this file.
|
static void |
main(java.lang.String[] args)
Simple usage example.
|
void |
mark(int readAheadLimit) |
java.lang.String |
next()
Return the next line in the multi-file.
|
void |
putback(java.lang.String line)
Puts the specified line back in the input.
|
int |
read() |
int |
read(char[] cbuf,
int off,
int len) |
@Nullable java.lang.String |
readLine()
Read a line, ignoring comments and processing includes.
|
void |
remove()
remove() is not supported.
|
void |
reset() |
void |
setEntryStartStop(java.util.regex.Pattern entryStartRegex,
java.util.regex.Pattern entryStopRegex)
Set the regular expressions for the start and stop of long entries (multiple lines that are
read as a group by getEntry()).
|
void |
setEntryStartStop(java.lang.String entryStartRegex,
java.lang.String entryStopRegex)
Set the regular expressions for the start and stop of long entries (multiple lines that are
read as a group by getEntry()).
|
void |
setLineNumber(int lineNumber)
Set the current line number in the current file.
|
long |
skip(long n) |
public @MonotonicNonNull java.util.regex.Pattern entryStartRegex
If the first line of an entry matches this regexp, then the entry is terminated by: entryStopRegex, another line that matches entryStartRegex (even not following a
newline), or the end of the current file.
Otherwise, the first line of an entry does NOT match this regexp (or the regexp is null), in which case the entry is terminated by a blank line or the end of the current file.
public @MonotonicNonNull java.util.regex.Pattern entryStopRegex
entryStartRegexpublic EntryReader(java.io.InputStream in,
java.lang.String charsetName,
java.lang.String filename,
@Nullable java.lang.String commentRegexString,
@Nullable java.lang.String includeRegexString)
throws java.io.UnsupportedEncodingException
in - source from which to read entriescharsetName - the character set to usefilename - non-null file name for stream being readcommentRegexString - regular expression that matches comments. Any text that matches
commentRegex is removed. A line that is entirely a comment is ignored.includeRegexString - regular expression that matches include directives. The expression
should define one group that contains the include file name.java.io.UnsupportedEncodingException - if the charset encoding is not supportedEntryReader(InputStream,String,String,String)public EntryReader(java.io.InputStream in,
java.lang.String charsetName,
java.lang.String filename)
throws java.io.UnsupportedEncodingException
in - the InputStreamcharsetName - the character set to usefilename - the file namejava.io.UnsupportedEncodingException - if the charset encoding is not supportedEntryReader(InputStream,String,String,String)public EntryReader(java.io.InputStream in,
java.lang.String filename,
@Nullable java.lang.String commentRegexString,
@Nullable java.lang.String includeRegexString)
in - source from which to read entriesfilename - non-null file name for stream being readcommentRegexString - regular expression that matches comments. Any text that matches
commentRegex is removed. A line that is entirely a comment is ignored.includeRegexString - regular expression that matches include directives. The expression
should define one group that contains the include file name.public EntryReader(java.io.InputStream in,
java.lang.String filename)
in - the InputStreamfilename - the file nameEntryReader(InputStream,String,String,String,String)public EntryReader(java.io.InputStream in)
in - the InputStreamEntryReader(InputStream,String,String,String)public EntryReader(java.io.Reader reader,
java.lang.String filename,
@Nullable java.lang.String commentRegexString,
@Nullable java.lang.String includeRegexString)
reader - source from which to read entriesfilename - file name corresponding to reader, for use in error messagescommentRegexString - regular expression that matches comments. Any text that matches
commentRegex is removed. A line that is entirely a comment is ignoredincludeRegexString - regular expression that matches include directives. The expression
should define one group that contains the include file namepublic EntryReader(java.io.Reader reader)
reader - source from which to read entriesEntryReader(Reader,String,String,String)public EntryReader(java.nio.file.Path path,
@Nullable java.lang.String commentRegex,
@Nullable java.lang.String includeRegex)
throws java.io.IOException
path - initial file to readcommentRegex - regular expression that matches comments. Any text that matches
commentRegex is removed. A line that is entirely a comment is ignored.includeRegex - regular expression that matches include directives. The expression should
define one group that contains the include file name.java.io.IOException - if there is a problem reading the filepublic EntryReader(java.nio.file.Path path)
throws java.io.IOException
path - the file to readjava.io.IOException - if there is a problem reading the fileEntryReader(File,String,String)public EntryReader(java.nio.file.Path path,
java.lang.String charsetName)
throws java.io.IOException
path - the file to readcharsetName - the character set to usejava.io.IOException - if there is a problem reading the fileEntryReader(Path,String,String)public EntryReader(java.io.File file,
@Nullable java.lang.String commentRegex,
@Nullable java.lang.String includeRegex)
throws java.io.IOException
file - initial file to readcommentRegex - regular expression that matches comments. Any text that matches
commentRegex is removed. A line that is entirely a comment is ignored.includeRegex - regular expression that matches include directives. The expression should
define one group that contains the include file name.java.io.IOException - if there is a problem reading the filepublic EntryReader(java.io.File file)
throws java.io.IOException
file - the file to readjava.io.IOException - if there is a problem reading the fileEntryReader(File,String,String)public EntryReader(java.io.File file,
java.lang.String charsetName)
throws java.io.IOException
file - the file to readcharsetName - the character set to usejava.io.IOException - if there is a problem reading the fileEntryReader(File,String,String)public EntryReader(java.lang.String filename,
@Nullable java.lang.String commentRegex,
@Nullable java.lang.String includeRegex)
throws java.io.IOException
filename - initial file to readcommentRegex - regular expression that matches comments. Any text that matches commentRegex is removed. A line that is entirely a comment is ignored.includeRegex - regular expression that matches include directives. The expression should
define one group that contains the include file name.java.io.IOException - if there is a problem reading the fileEntryReader(File,String,String)public EntryReader(java.lang.String filename)
throws java.io.IOException
filename - source from which to read entriesjava.io.IOException - if there is a problem reading the fileEntryReader(String,String,String)public EntryReader(java.lang.String filename,
java.lang.String charsetName)
throws java.io.IOException
filename - source from which to read entriescharsetName - the character set to usejava.io.IOException - if there is a problem reading the fileEntryReader(String,String,String)public @Nullable java.lang.String readLine(@GuardSatisfied EntryReader this)
throws java.io.IOException
readLine in class java.io.LineNumberReaderjava.io.IOExceptionpublic java.util.Iterator<java.lang.String> iterator()
Warning: This does not return a fresh iterator each time. The iterator is a singleton, the same one is returned each time, and a new one can never be created after it is exhausted.
iterator in interface java.lang.Iterable<java.lang.String>public boolean hasNext(@GuardSatisfied EntryReader this)
hasNext in interface java.util.Iterator<java.lang.String>public java.lang.String next(@GuardSatisfied EntryReader this)
next in interface java.util.Iterator<java.lang.String>java.util.NoSuchElementException - at end of filepublic void remove(@GuardSatisfied EntryReader this)
remove in interface java.util.Iterator<java.lang.String>public @Nullable EntryReader.Entry getEntry(@GuardSatisfied EntryReader this) throws java.io.IOException
entryStartRegex (see setEntryStartStop(java.lang.String, java.lang.String)). If no more
entries are available, returns null.java.io.IOException - if there is a problem reading the filepublic java.lang.String getFileName(@GuardSatisfied EntryReader this)
public int getLineNumber(@GuardSatisfied EntryReader this)
getLineNumber in class java.io.LineNumberReaderpublic void setLineNumber(@GuardSatisfied EntryReader this,
int lineNumber)
setLineNumber in class java.io.LineNumberReaderlineNumber - new line number for the current filepublic void setEntryStartStop(@GuardSatisfied EntryReader this,
java.lang.String entryStartRegex,
java.lang.String entryStopRegex)
entryStartRegex - regular expression that starts a long entryentryStopRegex - regular expression that ends a long entrypublic void setEntryStartStop(@GuardSatisfied EntryReader this,
java.util.regex.Pattern entryStartRegex,
java.util.regex.Pattern entryStopRegex)
entryStartRegex - regular expression that starts a long entryentryStopRegex - regular expression that ends a long entrypublic void putback(@GuardSatisfied EntryReader this,
java.lang.String line)
line - the line to be put back in the inputpublic void mark(@GuardSatisfied EntryReader this,
int readAheadLimit)
mark in class java.io.LineNumberReaderpublic int read(@GuardSatisfied EntryReader this)
read in class java.io.LineNumberReaderpublic int read(@GuardSatisfied EntryReader this,
char[] cbuf,
int off,
int len)
read in class java.io.LineNumberReaderpublic void reset(@GuardSatisfied EntryReader this)
reset in class java.io.LineNumberReaderpublic long skip(@GuardSatisfied EntryReader this,
long n)
skip in class java.io.LineNumberReaderpublic static void main(java.lang.String[] args)
throws java.io.IOException
args - command-line arguments: filename [commentRegex [includeRegex]]java.io.IOException - if there is a problem reading a file