一、概述
Writer 是写入字符流的抽象类。
OutputStreamWriter(OutputStream out):输出转换流,创建使用默认字符编码的OutputStreamWriter。
OutputStreamWriter(OutputStream out, String charsetName):创建使用指定字符编码的OutputStreamWriter。
字符流的底层还是用字节流进行读写的,字符流仅仅是做了字节和字符的转换。Reader是用于读取字符流的抽象类。
InputStreamReader(InputStream in):输入转换流,创建默认字符集的InputStreamReader。
InputStreamReader(InputStream in, String charsetName):创建使用指定字符编码的InputStreamReader。
二、字符流写数据
- 字符流写文本数据
方法 | 说明 |
publicvoid write(int c) | 写出一个字符 |
publicvoid write(char[] cbuf) | 写出字符数组 |
publicvoid write(char[] cbuf, int off, int len) | 写出字符数组cbuf中,从off开始,共len个字符 |
publicvoid write(String str) | 写出字符串 |
publicvoid write(String str, int off, int len) | 写出字符串,从off开始,共len个字符 |
eg:
//publicvoid write(int c) 写出一个字符
public static void main(String[] args) {
OutputStreamWriter outputStreamWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test02.txt"), "UTF-8");
outputStreamWriter.write('a');
outputStreamWriter.write('b');
outputStreamWriter.write('c');
outputStreamWriter.write('猫');
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//publicvoid write(char[] cbuf, int off, int len) 写出字符数组cbuf中,从off开始,共len个字符
public static void main(String[] args) {
OutputStreamWriter outputStreamWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test02.txt"), "UTF-8");
char[] chars = {'a','b','c','猫'};
outputStreamWriter.write(chars,2,2);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//publicvoid write(char[] cbuf) 写出字符数组
public static void main(String[] args) {
OutputStreamWriter outputStreamWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test02.txt"), "UTF-8");
char[] chars = {'a','b','c','猫'};
outputStreamWriter.write(chars);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStreamWriter != null) {
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//publicvoid write(String str) 写出字符串
public static void main(String[] args) {
OutputStreamWriter outputStreamWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"),"UTF-8");
outputStreamWriter.write("好好学习,天天向上");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outputStreamWriter != null){
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//publicvoid write(String str, int off, int len) 写出字符串,从off开始,共len个字符
public static void main(String[] args) {
OutputStreamWriter outputStreamWriter = null;
try {
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"),"UTF-8");
outputStreamWriter.write("好好学习,天天向上", 4, 4);
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outputStreamWriter != null){
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
三、字符流读数据
方法 | 说明 |
int read() | 一次读一个字符数据 |
int read(char[] cbuf) | 一次读一个字符数组数据 |
eg:
//int read() 一次读一个字符数据
public static void main(String[] args) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"), "UTF-8");
System.out.println((char)inputStreamReader.read());
System.out.println((char)inputStreamReader.read());
System.out.println((char)inputStreamReader.read());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//int read(char[] cbuf) 一次读一个字符数组数据
public static void main(String[] args) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"),"UTF-8");
char[] chars = new char[1024];
int length;
while ((length = inputStreamReader.read(chars)) != -1){
System.out.println(new String(chars,0,length));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
四、字符流拷贝文本文件
eg:
public static void main(String[] args) {
InputStreamReader inputStreamReader = null;
OutputStreamWriter outputStreamWriter = null;
try {
inputStreamReader = new InputStreamReader(new FileInputStream("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
outputStreamWriter = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\test\\test02\\test01.txt"));
char[] chars = new char[1024];
int length;
while ((length = inputStreamReader.read(chars)) != -1){
outputStreamWriter.write(chars,0, length);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStreamReader != null){
try {
inputStreamReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(outputStreamWriter != null){
try {
outputStreamWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
五、FileWriter和FileReader
eg:
public static void main(String[] args) {
FileReader fileReader = null;
FileWriter fileWriter = null;
try {
fileReader = new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt");
fileWriter = new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test02\\test01.txt");
char[] chars = new char[1024];
int length;
while ((length = fileReader.read(chars)) != -1){
fileWriter.write(chars,0,length);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fileReader != null){
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileWriter != null){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
六、字符缓冲流
- 概述
(1)BufferedReader:字符缓存输入流。从字符输入流读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
FileReader:内部使用InputStreamReader,解码过程,byte -> char,默认缓存大小为8K。
BufferedReader:默认缓存大小为8K,但可以手动指定缓存大小,把数据读取到缓存中,减少每次转换过程,效率更高。
(2)BufferedWriter:字符缓存输出流。将文本写入字符输出流,缓存各个字符,从而提供单个字符、数组和字符串的高效写入。
FileWriter:内部使用InputStreamWriter,解码过程,byte -> char,默认缓存大小为8K。
BufferedWriter:默认缓存大小为8K,但可以手动指定缓存大小,把数据读取到缓存中,减少每次转换过程,效率更高。
(3)字符缓冲流与字节缓冲流的思想一样。
- 字符缓冲流的读写操作
eg:
public static void main(String[] args) {
//写数据
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
bufferedWriter.write("好好学习,天天向上");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedWriter != null){
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读数据
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
char[] chars = new char[1024];
int length;
while ((length = bufferedReader.read(chars)) != -1){
System.out.println(new String(chars,0,length));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 字符缓冲流的换行操作
(1)BufferedWriter
方法 | 说明 |
void newLine() | 写一行分隔符,行分隔符字符串由系统属性定义 |
(2)BufferedReader
方法 | 说明 |
String readLine() | 读一行文字。结果包含行的内容的字符串,不包括任何行终止字符,如果流的结尾已经到达,则返回null |
eg:
public static void main(String[] args) {
//写数据
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
for(int i=1; i<=10; i++){
bufferedWriter.write("qizekj"+i);
bufferedWriter.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//读数据
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
String str;
while ((str = bufferedReader.readLine()) != null) {
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 字符缓冲流复制文本文件
eg:
public static void main(String[] args) {
BufferedReader bufferedReader = null;
BufferedWriter bufferedWriter = null;
try {
bufferedReader = new BufferedReader(new FileReader("C:\\Users\\Administrator\\Desktop\\test\\test01\\test01.txt"));
bufferedWriter = new BufferedWriter(new FileWriter("C:\\Users\\Administrator\\Desktop\\test\\test02\\test01.txt"));
String str;
while ((str = bufferedReader.readLine()) != null) {
bufferedWriter.write(str);
bufferedWriter.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedWriter != null) {
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}