这是fw.close的作用。请运行代码。- package com.snow;
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- public class BufferedWriterDemo {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- FileWriter fw = new FileWriter("d:\\Demo.txt");
- BufferedWriter bufw = new BufferedWriter(fw);
-
- fw.write("aaaaaaaaaaa");
- bufw.write("abcde");
- bufw.newLine();
- bufw.write("12334");
-
- //bufw.close();
- fw.close(); //为什么关闭BufferedWriter 就是关闭了FileWriter的流对象?取消了fw.close();的注释后,似乎这语句没起任何作用
- }
- }
复制代码 官方的解释:注意红色的部分
close
public void close()
throws IOException
Description copied from class: Writer
Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.
Specified by:
close in interface Closeable
Specified by:
close in interface AutoCloseable
Specified by:
close in class Writer
Throws:
IOException - If an I/O error occurs
|