Java CharArrayReader markSupported()方法

java.io.CharArrayReader.markSupported() 用于判断流是否支持mark()操作。

1 语法

public boolean markSupported()

2 参数

3 返回值

如果流支持mark()调用,则该方法返回true。

4 示例 

package com.yiidian;

/**
 * 一点教程网: http://www.yiidian.com
 */
/**
 * java.io.CharArrayReader.markSupported()方法的例子
 */
import java.io.CharArrayReader;
import java.io.IOException;

public class Demo {
    public static void main(String[] args) {
        CharArrayReader car = null;
        char[] ch = {'A', 'B', 'C', 'D', 'E'};

        try {
            // create new character array reader
            car = new CharArrayReader(ch);

            // verifies if the stream support mark() method
            boolean bool = car.markSupported();
            System.out.println("Is mark supported : "+bool);
            System.out.println("Proof:");

            // read and print the characters from the stream
            System.out.println(car.read());
            System.out.println(car.read());

            // mark() is invoked at this position
            car.mark(0);
            System.out.println("Mark() is invoked");
            System.out.println(car.read());
            System.out.println(car.read());

            // reset() is invoked at this position
            car.reset();
            System.out.println("Reset() is invoked");
            System.out.println(car.read());
            System.out.println(car.read());
            System.out.println(car.read());

        } catch(IOException e) {
            // if I/O error occurs
            e.printStackTrace();
        } finally {
            // releases any system resources associated with the stream
            if(car!=null)
                car.close();
        }
    }
}

输出结果为:

Is mark supported : true
Proof:
65
66
Mark() is invoked
67
68
Reset() is invoked
67
68
69

热门文章

优秀文章