Java - FilterReader markSupported() method



Description

The Java FilterReader markSupported() method marks the present position of the stream.

Declaration

Following is the declaration for java.io.FilterReader.markSupported() method −

public void markSupported()

Parameters

NA

Return Value

This method returns true if this stream support the mark operation.

Exception

NA

Example - Usage of FilterReader markSupported() method

The following example shows the usage of Java FilterReader markSupported() method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FilterReader;
import java.io.Reader;
import java.io.StringReader;

public class FilterReaderDemo {
   public static void main(String[] args) throws Exception {
      FilterReader fr = null;
      Reader r = null;      
      boolean bool = false;
      
      try {
         // create new reader
         r = new StringReader("ABCDEF");
          
         // create new filter reader
         fr = new FilterReader(r) {
         };
         
         // tests if the filter reader supports mark()
         bool = fr.markSupported();
         
         // prints
         System.out.print("If mark() supported? "+bool);
         
      } catch(Exception e) {
         // if any I/O error occurs
         e.printStackTrace();
      } finally {
         // releases system resources associated with this stream
         if(r!=null)
            r.close();
         if(fr!=null)
            fr.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

If mark() supported? true

Example - Writing a String as Bytes to a File

The following example shows the usage of Java FilterReader markSupported() method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FilterReaderDemo {
   public static void main(String[] args) {
      String data = "Hello, FileOutputStream!";

      try (FileOutputStream fos = new FileOutputStream("output.txt")) {
         // Convert string to byte array
         byte[] byteData = data.getBytes();

         // Write byte array to the file
         fos.write(byteData);

         System.out.println("Data written successfully to output.txt.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Data written successfully to output.txt.

Explanation

  • Converts a string into a byte array using .getBytes().

  • Creates a FileOutputStream for "output.txt".

  • Writes the byte array to the file.

  • The file will be overwritten if it already exists.

Example - Writing Binary Data (Image or File Content)

The following example shows the usage of Java FilterReader markSupported() method.

FilterReaderDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.IOException;

public class FilterReaderDemo {
   public static void main(String[] args) {
      byte[] binaryData = {65, 66, 67, 68, 69}; // ASCII values of 'A', 'B', 'C', 'D', 'E'

      try (FileOutputStream fos = new FileOutputStream("binary_output.bin")) {
         // Writing byte array to the file
         fos.write(binaryData);

         System.out.println("Binary data written to binary_output.bin.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Binary data written to binary_output.bin.

Explanation

  • Creates a byte array {65, 66, 67, 68, 69}, which corresponds to "ABCDE".

  • Creates a FileOutputStream for "binary_output.bin".

  • Writes the byte array to the file.

  • The file stores raw binary data instead of text.

java_io_FilterReader.htm