GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - PushbackReader class
Selected Reading
Java - PushbackReader classIntroductionThe Java PushbackReader class is a character-stream reader that allows characters to be pushed back into the stream. Class declarationFollowing is the declaration for Java.io.PushbackReader class − public class PushbackReader extends FilterReader FieldFollowing are the fields for Java.io.PushbackReader class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Properly closing a PushbackReaderThe following example shows the usage of PushbackReader close() method. PushbackReaderDemo.javapackage com.tutorialspoint;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
public class PushbackReaderDemo {
public static void main(String[] args) {
try (PushbackReader reader = new PushbackReader(new StringReader("Hello, Java!"))) {
int ch = reader.read();
System.out.println("First character read: " + (char) ch); // Output: H
// Push the character back
reader.unread(ch);
// Read it again
System.out.println("After unread: " + (char) reader.read()); // Output: H
// The reader will be automatically closed here due to try-with-resources
} catch (IOException e) {
e.printStackTrace();
}
}
}
OutputLet us compile and run the above program, this will produce the following result− First character read: H After unread: H Explanation
Example - Directly calling mark() on PushbackReaderThe following example shows the usage of PushbackReader markSupported() method. PushbackReaderDemo.javapackage com.tutorialspoint;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
public class PushbackReaderDemo {
public static void main(String[] args) {
try (PushbackReader reader = new PushbackReader(new StringReader("Hello, world!"))) {
System.out.println("markSupported: " + reader.markSupported()); // false
reader.mark(10); // This line will throw IOException
} catch (IOException e) {
System.out.println("Exception caught: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result− markSupported: false Exception caught: mark/reset not supported Example - Attempting Mark Without Checking markSupported()The following example shows the usage of PushbackReader markSupported() method. PushbackReaderDemo.javapackage com.tutorialspoint;
import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;
public class PushbackReaderDemo {
public static void main(String[] args) {
try (PushbackReader reader = new PushbackReader(new StringReader("World"))) {
System.out.println("markSupported: " + reader.markSupported()); // false
// Attempting to mark anyway – will throw IOException
reader.mark(5);
} catch (IOException e) {
System.out.println("Exception occurred: " + e.getMessage());
}
}
}
OutputLet us compile and run the above program, this will produce the following result− markSupported: false Exception occurred: mark/reset not supported Explanation
Advertisements
| ||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |