Java Reader close()方法

java.io.Reader.close() 关闭流并释放与其关联的所有系统资源。一旦关闭流,再调用read(),ready(),mark(),reset()或skip()方法将引发IOException异常。

1 语法

public abstract void close()

2 参数

3 返回值

4 示例 

package com.yiidian;

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

public class Demo {
    public static void main(String[] args) {
        String s = "Hello World";

        // create a new StringReader
        Reader reader = new StringReader(s);

        try {
            // read the first five chars
            for (int i = 0; i < 5; i++) {
                char c = (char) reader.read();
                System.out.print("" + c);
            }

            // change line
            System.out.println();

            // close the stream
            System.out.println("Closing Stream...");
            reader.close();
            System.out.println("Stream Closed.");

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

输出结果为:

Hello
Closing Stream...
Stream Closed.

热门文章

优秀文章