InputStream for Marking and Resetting Methods
public synchronized void mark(int readLimit)
public synchronized void reset()throws IOException
public boolean markSupported()
The boolean markSupported() method returns true if this stream supports marking and false if it does not.
Assuming the stream does support marking, the mark() method places a bookmark at the current position in the stream.
You can rewind the stream to this position later with reset() as long as you have not read more than readLimit bytes.
Marking and Resetting
It is often useful to be able to read a few bytes and then back up and reread them.
For example, in a Java compiler, you do not know for sure whether you are reading the token <, <<, or <<= until you have read one too many characters. It would be useful to be able to back up and reread the token once you know which token you have read.
Compiler design and other parsing problems provide many more examples, and this need occurs in other domains as well.
Some (but not all) input streams allow you to mark a particular position in the stream and then return to it.
Three methods in the java.io.InputStream class handle marking and resetting:
public synchronized void mark(int readLimit)
public synchronized void reset() throws IOException
public boolean markSupported()
Java I/O
The boolean markSupported() method returns true if this stream supports marking and false if it does not.
If marking is not supported, reset() throws an IOException and mark() does nothing. Assuming the stream does support marking,
the mark() method places a bookmark at the current position in the stream. You can rewind the stream to this position
later with reset() as long as you haven't read more than readLimit bytes.
There can be only one mark in the stream at any given time. Marking a second location erases the first mark.
The only two input stream classes in java.io that always support marking are BufferedInputStream (of which System.in is an instance) and
ByteArrayInputStream. However, other input streams, like DataInputStream , may support marking if they're chained to a buffered input stream first.
Scanner Examples
The addition of Scanner to Java makes what was formerly a tedious task into an easy one.
To understand why, let uslook at some examples. The following program averages a list of numbers entered at the keyboard:
// Use Scanner to compute an average of the values.
import java.util.*;
class AvgNums {
public static void main(String args[]) {
Scanner conin = new Scanner(System.in);
int count = 0;
double sum = 0.0;
System.out.println("Enter numbers to average.");
// Read and sum numbers.
while(conin.hasNext()) {
if(conin.hasNextDouble()) {
sum += conin.nextDouble();
count++;
}
else {
String str = conin.next();
if(str.equals("done"))
break;
else {
System.out.println("Data format error.");
return;
}
}
}
System.out.println("Average is " + sum / count);
}
}
The program reads numbers from the keyboard, summing them in the process, until the
user enters the string "done". It then stops input and displays the average of the numbers.
Here is a sample run:
Enter numbers to average.
1.2
2
3.4
4
done
Average is 2.65