一、字符流
主要操作文本;
Writer -- 字符输出流的基类(抽象类)
Reader -- 字符输入流的基类(抽象类)
OutputStreamWriter extends Writer -- 字符输出转换流
InputStreamReader extends Reader -- 字符输入转换流
ps:将字节流 --> 字符流
//1.创建流对象
FileOutputStream fos = new FileOutputStream("IO流.txt",true);
//设置编码格式 GBK或UTF-8
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
//2.写入,四种方式
osw.write("123木头人");
osw.write(97);
osw.write("123木头人".toCharArray(), 1, 3);
osw.write("123木头人".toCharArray());
//3.关流
osw.close();
//1.创建流对象
FileInputStream fis = new FileInputStream("IO流.txt");
InputStreamReader isr = new InputStreamReader(fis, "GBK");
//2.读取
char[] c = new char[1024];
int len;
while((len = isr.read(c))!=-1) {
System.out.println(new String(c, 0, len));
}
//3.关流
fis.close();
isr.close();
//需求:拷贝文本
//1.创建流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("IO流.txt"), "UTF-8");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("IO流2.txt"),"UTF-8");
//2.拷贝
char[] c = new char[1024];
int len;
while((len = isr.read(c))!=-1) {
osw.write(c, 0, len);
}
//3.关流
isr.close();
osw.close();
FileWriter extends OutputStreamWriter -- 文件字符输出流
FileReader extends InputStreamReader -- 文件字符输入流
//1.创建流对象
FileWriter fw = new FileWriter("test01.txt");
//2.写入
fw.write("一个大傻子");
fw.write("一个大傻子".toCharArray());
//3.关流
fw.close();
//1.创建流对象
FileReader fr = new FileReader("IO流.txt");
//2.读入
char[] c = new char[1024];
int len;
while((len = fr.read(c))!=-1) {
System.out.println(new String(c, 0, len));
}
//3.关流
fr.close();
//需求:拷贝文本
//1.创建流对象
FileReader fr = new FileReader("IO流.txt");
FileWriter fw = new FileWriter("IO流3.txt");
//2.拷贝
char[] c = new char[1024];
int len;
while((len = fr.read(c))!=-1) {
fw.write(c, 0, len);
}
//3.关流
fr.close();
fw.close();
BufferedWr