📅  最后修改于: 2020-09-27 04:50:06             🧑  作者: Mango
Java BufferedWriter类用于为Writer实例提供缓冲。它使性能快速。它继承了Writer类。缓冲字符用于有效地写入单个数组,字符和字符串。
让我们看一下Java.io.BufferedWriter类的声明:
public class BufferedWriter extends Writer
Constructor | Description |
---|---|
BufferedWriter(Writer wrt) | It is used to create a buffered character output stream that uses the default size for an output buffer. |
BufferedWriter(Writer wrt, int size) | It is used to create a buffered character output stream that uses the specified size for an output buffer. |
Method | Description |
---|---|
void newLine() | It is used to add a new line by writing a line separator. |
void write(int c) | It is used to write a single character. |
void write(char[] cbuf, int off, int len) | It is used to write a portion of an array of characters. |
void write(String s, int off, int len) | It is used to write a portion of a string. |
void flush() | It is used to flushes the input stream. |
void close() | It is used to closes the input stream |
让我们看一个使用Java BufferedWriter将数据写入文本文件testout.txt的简单示例。
package com.javatpoint;
import java.io.*;
public class BufferedWriterExample {
public static void main(String[] args) throws Exception {
FileWriter writer = new FileWriter("D:\\testout.txt");
BufferedWriter buffer = new BufferedWriter(writer);
buffer.write("Welcome to javaTpoint.");
buffer.close();
System.out.println("Success");
}
}
输出:
success
testout.txt:
Welcome to javaTpoint.