
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
Java - InputStream markSupported() method
Description
The Java InputStream markSupported() method checks whether the input stream supports marking and resetting using mark(int readLimit) and reset(). Returns true if the stream supports mark() and reset(), false if marking is not supported.
Declaration
Following is the declaration for java.io.InputStream.markSupported() method −
public boolean markSupported()
Parameters
NA
Return Value
The method returns true if this stream supports instance the mark and reset method.
Exception
NA
Example - Usage of InputStream markSupported() method
The following example shows the usage of Java InputStream markSupported() method.
InputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) throws Exception { InputStream is = null; boolean bool = false; try { // new input stream created is = new FileInputStream("test.txt"); // returns true if the mark()/reset() supported. bool = is.markSupported(); // prints System.out.print("Is mark()/reset() supported? "); System.out.print(bool); } catch(Exception e) { // if any I/O error occurs e.printStackTrace(); } finally { // releases system resources associated with this stream if(is!=null) is.close(); } } }
Output(Assuming test.txt contains "ABCDEF")
Let us compile and run the above program, this will produce the following result−
Is mark()/reset() supported? false
Example - Checking markSupported() for BufferedInputStream
The following example shows the usage of Java InputStream markSupported() method.
InputStreamDemo.java
package com.tutorialspoint; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) { try (InputStream inputStream = new BufferedInputStream(new FileInputStream("example.txt"))) { System.out.println("Mark supported? " + inputStream.markSupported()); inputStream.mark(5); // Mark the position System.out.print((char) inputStream.read()); // Read a character System.out.print((char) inputStream.read()); // Read another character inputStream.reset(); // Reset back to the mark System.out.print((char) inputStream.read()); // Read again } catch (IOException e) { e.printStackTrace(); } } }
Output(if example.txt contains "Java")
Let us compile and run the above program, this will produce the following result−
Mark supported? true Jav
Explanation
Uses BufferedInputStream, which supports marking.
Calls markSupported(), which returns true.
Marks a position, reads characters, and resets to the mark.
Example - Checking markSupported() for FileInputStream (Mark Not Supported)
The following example shows the usage of Java InputStream markSupported() method.
InputStreamDemo.java
package com.tutorialspoint; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class InputStreamDemo { public static void main(String[] args) { try (InputStream inputStream = new FileInputStream("example.txt")) { System.out.println("Mark supported? " + inputStream.markSupported()); inputStream.mark(10); // Attempting to mark (Has no effect) System.out.print((char) inputStream.read()); // Read a character inputStream.reset(); // This will throw an IOException since FileInputStream does NOT support mark/reset } catch (IOException e) { System.out.println("Reset failed: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result−
Mark supported? False HReset failed: mark/reset not supported
Explanation
Uses FileInputStream, which does not support marking.
Calls markSupported(), which returns false.
Attempting reset() throws an IOException.