Package org.cis1200
Class FileLineIterator
java.lang.Object
org.cis1200.FileLineIterator
FileLineIterator provides a useful wrapper around Java's provided
BufferedReader and provides practice with implementing an Iterator. Your
solution should not read the entire file into memory at once, instead reading
a line whenever the next() method is called.
Note: Any IOExceptions thrown by readers should be caught and handled properly. Do not use the ready() method from BufferedReader.
-
Constructor Summary
ConstructorsConstructorDescriptionFileLineIterator
(BufferedReader reader) Creates a FileLineIterator for the reader.FileLineIterator
(String filePath) Creates a FileLineIterator from a provided filePath by creating a FileReader and BufferedReader for the file. -
Method Summary
Modifier and TypeMethodDescriptionstatic BufferedReader
fileToReader
(String filePath) Takes in a filename and creates a BufferedReader.boolean
hasNext()
Returns true if there are lines left to read in the file, and false otherwise.next()
Returns the next line from the file, or throws a NoSuchElementException if there are no more strings left to return (i.e.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface java.util.Iterator
forEachRemaining, remove
-
Constructor Details
-
FileLineIterator
Creates a FileLineIterator for the reader. Fill out the constructor so that a user can instantiate a FileLineIterator. Feel free to create and instantiate any variables that your implementation requires here. See recitation and lecture notes for guidance.If an IOException is thrown by the BufferedReader, then hasNext should return false.
The only method that should be called on BufferedReader is readLine() and close(). You cannot call any other methods.
- Parameters:
reader
- - A reader to be turned to an Iterator- Throws:
IllegalArgumentException
- if reader is null
-
FileLineIterator
Creates a FileLineIterator from a provided filePath by creating a FileReader and BufferedReader for the file.DO NOT MODIFY THIS METHOD.
- Parameters:
filePath
- - a string representing the file- Throws:
IllegalArgumentException
- if filePath is null or if the file doesn't exist
-
-
Method Details
-
fileToReader
Takes in a filename and creates a BufferedReader. See Java's documentation for BufferedReader to learn how to construct one given a path to a file.- Parameters:
filePath
- - the path to the CSV file to be turned to a BufferedReader- Returns:
- a BufferedReader of the provided file contents
- Throws:
IllegalArgumentException
- if filePath is null or if the file doesn't exist
-
hasNext
public boolean hasNext()Returns true if there are lines left to read in the file, and false otherwise.If there are no more lines left, this method should close the BufferedReader.
-
next
Returns the next line from the file, or throws a NoSuchElementException if there are no more strings left to return (i.e. hasNext() is false).This method also advances the iterator in preparation for another invocation. If an IOException is thrown during a next() call, your iterator should make note of this such that future calls of hasNext() will return false and future calls of next() will throw a NoSuchElementException
- Specified by:
next
in interfaceIterator<String>
- Returns:
- the next line in the file
- Throws:
NoSuchElementException
- if there is no more data in the file
-