学习JavaEE的日子 Day36 字符流
Day36
1.字符流
应用场景:操作纯文本数据
注意:字符流 = 字节流+编译器
编译器:可以识别中文字符和非中文字符,非中文字符获取1个字节(一个字节=一个字符),编译器会根据编码格式获取中文字符对应的字节数(GBK获取两个字节,UTF-8获取三个字节)
abstract class Reader – 字符输入流的基类(抽象类)
abstract class Writer – 字符输出流的基类(抽象类)
class InputStreamReader extends Reader – 字符输入转换流
class OutputStreamWriter extends Writer – 字符输出转换流
特点:将字节流转换为字符流,字符转换流是字节流和字符流的桥梁
class FileReader extends InputStreamReader – 文件字符输入流
class FileWriter extends OutputStreamWriter – 文件字符输出流
class BufferedReader extends Reader – 带缓冲区的字符输入流
class BufferedWriter extends Writer – 带缓冲区的字符输出流
默认缓冲区:8192字符 – new char[8192]
1.1 利用 字符输出转换流 向文件写入数据OutputStreamWriter
1)不处理异常的方式
2)文件存在的情况
3)文件不存在的情况
经验:所有的输出流,文件不存在的情况都会创建文件
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流对象(将文件字节输出流 转换为 字符输出转换流)
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("io.txt"));
//2.写入数据
//osw.write(97);//写入码值
//char[] cs = {'1','2','3','a','b','c','我','爱','你'};
//osw.write(cs);//写入字符数组
//osw.write(cs, 3, 4);//写入字符数组,偏移量,写入长度
//osw.write("123abc我爱你");//字符串
osw.write("123abc我爱你", 3, 4);//写入字符串,偏移量,写入长度
//3.关闭资源
osw.close();
}
}
- 文件末尾追加
经验:考虑基础流的构造方法
注意:使用字符转换流最好加上编码格式!!!!!!!
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象(将文件字节输出流 转换为 字符输出转换流)
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("io.txt",true),"UTF-8");
//2.写入数据
osw.write("123abc我爱你");
//3.关闭资源
osw.close();
}
}
1.2 利用 字符输入转换流 读取文件里的数据InputStreamReader
不处理异常
文件存在
文件不存在
经验:所有的输入流,在文件不存在时都会报错
public class Test05 {
public static void main(String[] args) throws IOException {
//1.创建流对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("io.txt"));
//2.读取数据
//int read;
//while((read = isr.read()) != -1){
// System.out.println((char)read);
//}
//2.读取数据
//isr.read(cs):读入cs数组长度的字符,将字符数据存入到数组中,并返回读取到的有效字节数,如果读取到文件末尾,则返回-1
char[] cs = new char[1024];
int len;
while((len = isr.read(cs)) != -1){
System.out.println(new String(cs, 0, len));
}
//3.关闭资源
isr.close();
}
}
1.3 拷贝文件
public class Copy {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("小说.txt"), "GBK");
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("copy.txt"), "GBK");
char[] cs = new char[1024];
int len;
while((len = isr.read(cs)) != -1){
osw.write(cs, 0, len);
}
isr.close();
osw.close();
}
}
1.4 利用 文件字符输出流 向文件写入数据FileWriter
public class Test01 {
public static void main(String[] args) throws IOException {
//1.创建流对象
//FileWriter fw = new FileWriter("io.txt");
//1.创建流对象
FileWriter fw = new FileWriter("io.txt", true);
//2.写入数据
fw.write("好好学习");
//3.关闭资源
fw.close();
}
}
1.5 利用 文件字符输入流 读取文件里的数据FileReader
public class Test02 {
public static void main(String[] args) throws IOException {
//1.创建流对象
FileReader fr = new FileReader("io.txt");
//2.读取数据
char[] cs = new char[1024];
int len;
while((len = fr.read(cs)) != -1){
System.out.println(new String(cs, 0, len));
}
//3.关闭资源
fr.close();
}
}
1.6 拷贝文件
public class Copy {
public static void main(String[] args) throws IOException {
FileReader fr = new FileReader("小说.txt");
FileWriter fw = new FileWriter("copy.txt");
char[] cs = new char[1024];
int len;
while((len = fr.read(cs)) != -1){
fw.write(cs, 0, len);
}
fr.close();
fw.close();
}
}
1.7 利用 带缓冲区的字符输出流 向文件写入数据BufferedWriter
public class Test01 {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
//1.创建流对象(文件字节输出流 -> 字符输出转换流 -> 带有缓冲区的字符输出流)
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("io.txt"), "GBK"));
//1.创建流对象(文件字节输出流 -> 字符输出转换流 -> 带有缓冲区的字符输出流)+ 末尾追加
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("io.txt",true), "GBK"));
//1.创建流对象(文件字符输出流 -> 带缓冲区的字符输出流)
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new FileWriter("io.txt"));
//1.创建流对象(文件字符输出流 -> 带缓冲区的字符输出流)+ 末尾追加
//默认缓冲区大小:8192字符
//BufferedWriter bw = new BufferedWriter(new FileWriter("io.txt",true));
//1.创建流对象(文件字符输出流 -> 带缓冲区的字符输出流)+ 末尾追加
//自定义缓冲区大小:2048字符
BufferedWriter bw = new BufferedWriter(new FileWriter("io.txt",true),2048);
//2.写入数据
bw.write("好好学习");
//3.关闭资源
bw.close();
}
}
1.8 利用 带有缓冲区的字符输入流 读取文件里的数据BufferedReader
public class Test02 {
public static void main(String[] args) throws UnsupportedEncodingException, IOException {
//1.创建流对象(文件字节输入流 -> 字符输入转换流 -> 带有缓冲区的字符输入流)
//默认缓冲区大小:8192字符
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("io.txt"), "GBK"));
//1.创建流对象(文件字节输入流 -> 字符输入转换流 -> 带有缓冲区的字符输入流)
//默认缓冲区大小:8192字符
//BufferedReader br = new BufferedReader(new FileReader("io.txt"));
//1.创建流对象(文件字节输入流 -> 字符输入转换流 -> 带有缓冲区的字符输入流)
//自定义缓冲区大小:2048字符
BufferedReader br = new BufferedReader(new FileReader("io.txt"),2048);
//2.读取数据
char[] cs = new char[1024];
int len;
while((len = br.read(cs)) != -1){
System.out.println(new String(cs, 0, len));
}
//3.关闭资源
br.close();
}
}
1.9 拷贝文件
public class Copy01 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("С˵.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
char[] cs = new char[1024];
int len;
while((len = br.read(cs)) != -1){
bw.write(cs, 0, len);
}
br.close();
bw.close();
}
}
另一种方法
public class Copy02 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("小说.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("copy.txt"));
// String readLine;
// while((readLine = br.readLine()) != null){
// bw.write(readLine);//写一行
// bw.newLine();//换行
// }
boolean bool = false;
String readLine;
while((readLine = br.readLine()) != null){
//第一行不换行
if(bool){
bw.newLine();//换行
}
bw.write(readLine);//写一行
bool = true;
}
br.close();
bw.close();
}
}
总结
1.InputStreamReader 和 OutputStreamWriter
理解转换流(字节流 -> 字符流)
2.FileReader、FileWriter
3…BufferedReader、BufferedWriter
注意:缓冲区大小-8192字符4.字符流继承图