Reputation: 169
I am a rank amateur when it comes to Java, so please pardon my question if it seems dumb :-P I have the following code which is designed to count the number of lines in a file:
while (scanNumOfLines.hasNextLine())
{
NumOfLines ++;
scanNumOfLines.nextLine();
}
System.out.println("NumOfLines = "+NumOfLines);
So it counts fine, but I want to re-use the scanner for another purpose, but the nextLine has moved to the last line of the file, and I want to reset it back to the first line.
(Instead, I had to use another scanner for the other purpose, and to me this seems less elegant than it should be.)
I'm sure there must be a scanner method that resets the counter to zero?
Thanks
CJ
Upvotes: 16
Views: 75020
Reputation: 21
I realize this has already been answered but if I'm looking up this stuff I'm sure someone else is too, so I thought I'd contribute the way I went about solving this problem: As my problem required me to read a file a number of times based on user input, my test class has an ArrayList of scanners that reads the file, then deletes itself once it's no longer needed. The way I have it set up there's never more than 1 scanner in the ArrayList, but ArrayList is useful in adding and removing objects.
public class TxtReader {
private boolean PROGRAM_CONTINUES = true;
private final int indexCount = 0;
File newFile = new File("Reader Example.txt");
ArrayList<Scanner> scanList = new ArrayList<Scanner>();
public TxtReader(Scanner UserInput) throws FileNotFoundException {
while(PROGRAM_CONTINUES) {
if (UserInput.next().equalsIgnoreCase("end")) {
// some arbitrary way of concluding the while loop
PROGRAM_CONTINUES = false;
break;
}
scanList.add(new Scanner(newFile));
// DO STUFF*********************************************
while(scanList.get(indexCount).hasNext()) {
System.out.println(scanList.get(indexCount).nextLine());
}
//******************************************************
scanList.get(indexCount).close();
//always close a scanner after you're done using it
scanList.remove(indexCount); // removes the now-unnecessary scanner
UserInput = new Scanner(System.in);
}
}
public static void main(String[] args) throws FileNotFoundException {
new TxtReader(new Scanner(System.in));
}
}
Upvotes: 2
Reputation: 11
File file = new File("StoreData.txt");
Scanner reader = new Scanner(new FileInputStream(file));
while (reader.hasNext()) {
k++;
reader.nextLine();
}
reader.close();
reader=null;
//reset scanner
reader=new Scanner(new FileInputStream(file));
while (reader.hasNext()) {
System.out.println(reader.nextLine());
}
Upvotes: 0
Reputation: 20760
This is impossible to do.
The reason to not include it, is the wide range of input types it supports. One example is streams. These don't store the results after they have been passed on, so they don't support resetting.
So the elegant way is to create a new Scanner
. If you give it many custom settings, create a factory method.
Upvotes: 18
Reputation: 14053
You could use a RandomAccessFile and use the method seek() to come back to first line.
Upvotes: -1
Reputation: 11592
You will have to redeclare the Scanner
. When you invoke nextLine()
, the line is removed from the buffer and effectively discarded from the Scanner
.
So, essentially, there is a method to do that: it's the constructor.
Scanner scanNumOfLines = new Scanner(myFile);
There is no "counter" in the Scanner
object. Instead think of it as more like a conveyor belt. The belt has no knowledge or care about what's on it. It just keeps spitting things out at you while there are items left on it. And once you take them, they're gone from it for good.
Upvotes: 9