Java OutputStreamWriter close()方法
java.io.OutputStreamWriter.close() 方法关闭流,但要先刷新它。一旦流已关闭,进一步调用write() or flush() 会导致一个IOException异常被抛出。关闭以前关闭的流无效。
1 语法
public void close()
2 参数
无
3 返回值
无
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* java.io.OutputStreamWriter.close()方法的例子
*/
public class Demo {
public static void main(String[] args) {
try {
// create a new OutputStreamWriter
OutputStream os = new FileOutputStream("test.txt");
OutputStreamWriter writer = new OutputStreamWriter(os);
// create a new FileInputStream to read what we write
FileInputStream in = new FileInputStream("test.txt");
// write something in the file
writer.write(70);
// flush the stream
writer.flush();
// read what we write
System.out.println("" + (char) in.read());
// close the stream
System.out.println("Closing Stream...");
writer.close();
System.out.println("Stream closed.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
输出结果为:
F
Closing Stream...
Stream closed.
热门文章
优秀文章