BufferedWriter class
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | java.lang.Object | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | - | - | java.io.Writer | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | - | - | java.io.BufferedWriter | | | | | | | | | | | | | | | | | | | | | | |
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |
Writes text to a character-output stream with buffer.
Revised from Open JDK source code
Create BufferedWriter from standard System.out
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String strLine = in.readLine();
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
out.write(strLine, 0, strLine.length());
out.flush();
in.close();
out.close();
}
}
Write String to a file with BufferedWriter
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
public class Main {
public static void main(String[] args) throws Exception {
Writer writer = null;
String text = "This is a text file";
File file = new File("write.txt");
writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
}
}