[java] Resetting the Scanner

Started by
6 comments, last by tebriel 18 years, 10 months ago
Is there anyway to reset the Scanner back to the start of the file besides closing it and opening it again?
Advertisement
Looking at the API docs, there doesn't appear to be one.
--But why would you want to? You should probably save whatever it is you need when the Scanner arrives at something you'll need later. Are you working with huge files or something?
Reading in a matrix of unknown size. First I use .nextLine() to get the number of rows, then .nextInt() for total items (divide that by rows to get columns), then create the array and read in the matrix. Its just kind of a pain to open and close the file so many times.
Why not dump the file into a byte array or byte buffer? (java.nio.ByteBuffer)
Its just a school assignment, so I'm not to worried about it.
couldn't you do something like:
InputStream fileInputStream = new FileInputStream(myFile);Scanner aScanner = new Scanner(fileInputStream);// ... do your stufffileInputStream.reset();// ... read in matrix
or wouldn't that work?
I'll give it a shot.

It works, but today the specs got changed so the size of the matrix is in the text file.

[Edited by - darkchrono4 on June 16, 2005 12:31:00 PM]
What Funkapotamus suggested is how I'd probably do it, actually I think it's the easier (and obviously faster) way to do what you want, rather than doing some kind of random access of a file on disk.

This topic is closed to new replies.

Advertisement