Java FilterInputStream.markSupported()方法

原文:https://www.studytonight.com/java-file-io/java-filterinputstream-marksupported-method

在本教程中,我们将学习 JavaFilterInputStream 类的 markSupported()方法。此方法用于验证当前筛选器输入流是否支持调用标记()和重置()方法。这是一种非静态方法,可在 java.io 包中获得。

句法

这是此方法的语法声明。它不接受任何参数。此方法的返回类型是布尔型的,即如果当前流支持 mark()和 reset()方法,则返回 true,否则返回 false。

public boolean markSupported()

示例 1:验证FilterInputStream中的标记支持

这里,我们正在检查当前流是否允许设置 mark 方法,如果当前流不允许 mark()方法,那么这个方法将返回 false 值。否则,它将返回真值。在本例中,此方法返回真值,这意味着 mark()方法支持给定的流。

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class StudyTonight
{
    public static void main(String args[])
    {
        try 
        {
            File file = new File("E:\\studytonight\\output.txt");

            FileInputStream fis = new FileInputStream(file);
            FilterInputStream filterIs = new BufferedInputStream(fis);

            System.out.println("is markSupported() ? " + filterIs.markSupported());

            fis.close();
            filterIs.close();
        } 
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }
}

是 markSupported()?真实的

示例 2:验证FilterInputStream中的标记支持

在给定的示例中,markSupported()方法返回了一个 false 值,该值指示当前流不支持 mark()方法,并且不需要调用 reset()方法

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class StudyTonight
{
    public static void main(String args[])
    {
        try 
        {
            File file = new File("E:\\studytonight\\myfile.txt");

            FileInputStream fis = new FileInputStream(file);
            FilterInputStream filterIs = new BufferedInputStream(fis);

            System.out.println("is markSupported() ? " + filterIs.markSupported());

            fis.close();
            filterIs.close();
        } 
        catch (IOException ex) 
        {
            ex.printStackTrace();
        }
    }
}

是 markSupported()?错误的

结论:

在本教程中,我们学习了 JavaFilterInputStream 类的 markSupported()方法,该方法检查当前FileInputStream是否支持 mark()和 reset()方法的调用。如果是,则返回布尔值 true,如果不是,则返回 false。它只能用类对象访问,如果我们试图用类名访问方法,我们会得到一个错误。