Buttercola: LinkedIn: Text file Iterable
Question:Implement a (Java) Iterable object that iterates lines one by one from a text file..
http://blog.csdn.net/craiglin1992/article/details/44778643
http://www.cnblogs.com/jxlgetter/p/4395098.html
http://plse.cs.washington.edu/daikon/staging-daikon/download/api/src-html/daikon/util/TextFile.html
Read full article from Buttercola: LinkedIn: Text file Iterable
Question:Implement a (Java) Iterable object that iterates lines one by one from a text file..
public
class
TextFile
implements
Iterable<String>
{
public
TextFile(String fileName) {
// please implement this
/** Begin reading the file, line by line.
* The returned Iterator.next() will return a line.
*/
@Override
public
Iterator<String> iterator() {
// please implement this
public
class
TextFile
implements
Iterable<String> {
Scanner scanner;
public
TextFile(String fileName) {
try
{
scanner =
new
Scanner(
new
File(fileName));
}
catch
(Exception e) {
}
}
/** Begin reading the file, line by line.
* The returned Iterator.next() will return a line.
*/
@Override
public
Iterator<String> iterator() {
return
new
TextFileIterator();
}
private
class
TextFileIterator
implements
Iterator<String> {
@Override
public
boolean
hasNext() {
return
scanner.hasNext();
}
@Override
public
String next() {
return
scanner.nextLine();
}
@Override
public
void
remove() {
throw
new
UnsupportedOperationException();
}
}
}
ANSWER: An implementation of this using bufferedReader:
http://stackoverflow.com/questions/8240071/what-is-mark-and-reset-in-bufferedreader
The
mark()
marking a particular point in a stream and reset()
resets the stream to the most recent mark. These methods provide a book-marking
feature that allows you to read ahead in the stream to inspect the upcoming data.
From this documentation:
The mark() method mark a position in the input to which the stream can be "reset" by calling the reset() method. The parameter readLimit is the number of chars that can be read from the stream after setting the mark before the mark becomes invalid. For example, if mark() is called with a read limit of 10, then when 11 chars of data are read from the stream before the reset() method is called, then the mark is invalid and the stream object instance is not required to remember the mark. Note that the number of chars that can be remembered by this method can be greater than the size of the internal read buffer. It is also not dependent on the subordinate stream supporting mark/reset functionality.
Streams can't naturally be "unread" to go backwards. Mark/reset provides a way to emulate that if needed.
Once you call
mark()
it will begin remembering data you read from that point forwards in memory. When you call reset()
it will jump back to the marked position, by satisfying future read()
s from the in the in-memory buffer. If you read past the end of that buffer, then it will seamlessly go back to reading fresh data from the underlying "in" reader that the BufferedReader was created with.http://plse.cs.washington.edu/daikon/staging-daikon/download/api/src-html/daikon/util/TextFile.html
Read full article from Buttercola: LinkedIn: Text file Iterable