| Constructor and Description |
|---|
| BufferedWriter(Writer out)
创建使用默认大小的输出缓冲区的缓冲字符输出流。 |
| BufferedWriter(Writer out, int sz)
创建一个新的缓冲字符输出流,使用给定大小的输出缓冲区。 |
| Modifier and Type | Method and Description |
|---|---|
| void | close()
关闭流,先刷新。 |
| void | flush()
刷新流。 |
| void | newLine()
写一行行分隔符。 |
| void | write(char[] cbuf, int off, int len)
写入字符数组的一部分。 |
| void | write(int c)
写一个字符 |
| void | write(String s, int off, int len)
写一个字符串的一部分。 |
例子:
File f = new File("d:/a.txt"); if(!f.exists())f.createNewFile(); //不存在,创建 FileOutputStream fos = new FileOutputStream(f, true); //true末尾写入,false覆盖 OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8"); //设置编码 BufferedWriter bw = new BufferedWriter(osw); //包装流,附加方法 bw.newLine(); //换行 bw.write("String字符串"); //将字符串写入文本 bw.flush(); //刷新流 bw.close(); //关闭流 osw.close(); fos.close();