javaSE25-- IO流--字符缓冲流BufferedReader/BufferedWriter
程序员文章站
2024-03-06 15:59:50
...
字符缓冲流
- BufferedReader/BufferedWriter
- 也是一种处理流,包装流
- bw.flush(); //清空缓冲区,把数据保存到文件中
bw.close(); - br.readLine();从缓冲字符流中读取一行,读到文件末尾返回null
从键盘上输入文本,把这些文本保存到文件中
private static void m3() throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("d:/xyz.txt"));
//使用BufferedReader对键盘输入流缓冲
// System.in是标准的输入设备,即键盘
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
while( line.length() > 0 ){
bw.write(line);
bw.newLine();
line = br.readLine();
}
br.close();
bw.close();
}
使用BufferedWriter保存文本到文件
private static void m2() throws IOException {
Writer out = new FileWriter("d:/abc.txt", true );
BufferedWriter bw = new BufferedWriter(out);
bw.write("我送你99朵玫瑰花儿");
// bw.flush(); //清空缓冲区,把数据保存到文件中
bw.close();
}
使用BufferedReader读取文本文件内容
private static void m1() throws IOException {
Reader in = new FileReader("d:/test08.java");
BufferedReader br = new BufferedReader(in);
//从缓冲字符流中读取一行,读到文件末尾返回null
String line = br.readLine();
while( line != null ){
System.out.println( line );
line = br.readLine();
}
br.close(); //把包装流关闭后, 被包装的流也会关闭
}
推荐阅读
-
字符缓冲输出流BufferedWriter与字符缓冲输入流BufferedReader:
-
javaSE25-- IO流--字符缓冲流BufferedReader/BufferedWriter
-
IO_处理流_字符缓冲流
-
java: ----- 多线程、IO流、字符流(Buffered缓冲加强版)Copy
-
Java IO流之字符缓冲流实例详解
-
IO流【字节流、字符流、缓冲流、转换流、序列化流】
-
Java IO流之字符缓冲流实例详解
-
IO流(字节流,字符流,缓冲流)
-
缓冲字符流 java.io.BufferedWriter ,java.io.BufferedReader,缓冲字符输出流:PrintWriter
-
JAVA-----IO篇四(缓冲流BufferedWriter以及BufferedReader、转换流outputStreamWriter以及inputStreamWriter)