|NO.Z.00081|——————————|BigDataEnd|——|Java&IO流.V08|——|Java.v08|IO流.v08|缓冲字符流|使用|
一、BufferedWriter类(重点)
### --- 基本概念
——> java.io.BufferedWriter类主要用于写入单个字符、字符数组以及字符串到输出流中。
二、常用的方法
| 方法声明 | 功能介绍 |
| BufferedWriter(Writer out) | 根据参数指定的引用来构造对象 |
| BufferedWriter(Writer out, int sz) | 根据参数指定的引用和缓冲区大小来构造对象 |
| void write(int c) | 写入单个字符到输出流中 |
| void write(char[] cbuf, int off, intlen) | 将字符数组cbuf中从下标off开始的len个字符写入输出流中 |
| void write(char[] cbuf) | 将字符串数组cbuf中所有内容写入输出流中 |
| void write(String s, int off, int len) | 将参数s中下标从off开始的len个字符写入输出流中 |
| void write(String str) | 将参数指定的字符串内容写入输出流中 |
| void newLine() | 用于写入行分隔符到输出流中 |
| void flush() | 刷新流 |
| void close() | 关闭流对象并释放有关的资源 |
三、BufferedReader类(重点)
### --- 基本概念
——> java.io.BufferedReader类用于从输入流中读取单个字符、字符数组以及字符串。
四、常用的方法
| 方法声明 | 功能介绍 |
| BufferedReader(Readerin) | 根据参数指定的引用来构造对象 |
| BufferedReader(Readerin, int sz) | 根据参数指定的引用和缓冲区大小来构造对象 |
| int read() | 从输入流读取单个字符,读取到末尾则返回-1, 否则返回实际读取到的字符内容 |
| int read(char[] cbuf, intoff, int len) | 从输入流中读取len个字符放入数组cbuf中下标从off开始的位置上,若读取到末尾则返回-1,否则返回实际读取到的字符个数 |
| int read(char[] cbuf) | 从输入流中读满整个数组cbuf |
| String readLine() | 读取一行字符串并返回,返回null表示读取到末尾 |
| void close() | 关闭流对象并释放有关的资源 |
五、编程代码
package com.yanqi.task17;
import java.io.*;
public class BufferedCharCopyTest {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
// 1.创建BufferedReader类型的对象与d:/a.txt文件关联
br = new BufferedReader(new FileReader("d:/a.txt"));
// 2.创建BufferedWriter类型的对象与d:/b.txt文件关联
bw = new BufferedWriter(new FileWriter("d:/b.txt"));
// 3.不断地从输入流中读取一行字符串并写入到输出流中
System.out.println("正在玩命地拷贝...");
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine(); // 当前系统中的行分隔符是:\r\n
}
System.out.println("拷贝文件成功!");
} catch (IOException e) {
e.printStackTrace();
} finally {
// 4.关闭流对象并释放有关的资源
if (null != bw) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
六、编译打印
D:\JAVA\jdk-11.0.2\bin\java.exe "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58500:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath E:\NO.Z.10000——javaproject\NO.H.00001.javase\javase\out\production\javase com.yanqi.task17.BufferedCharCopyTest
正在玩命地拷贝...
拷贝文件成功!
Process finished with exit code 0
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
浙公网安备 33010602011771号