字符流的介绍:
由于字节流操作中文不是特别的方便,所以Java就提供字符流
字符流 = 字节流 + 编码表
编码表
是一个系统支持的所有字符的集合,包括各国家文字、标点符号、图形符号、数字等
计算机要准确的存储和识别各种字符集符号,就需要进行字符编码,一套字符集必然至少有一套字符编码。 常见字符集有ASCII字符集、GBXXX字符集、Unicode字符集等
字符流中的编码解码问题:
InputStreamReader:是从字节流到字符流的桥梁
它读取字节,并使用指定的编码将其解码为字符
它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
OutputStreamWriter:是从字符流到字节流的桥梁
是从字符流到字节流的桥梁,使用指定的编码将写入的字符编码为字节
它使用的字符集可以由名称指定,也可以被明确指定,或者可以接受平台的默认字符集
构造方法:
实例代码:
public class CharStream_1 {
public static void main(String[] args) throws IOException {
//创建字符流对象
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day09\\java2.txt"),"GBK");
//写入数据
osw.write("中国");
osw.write("无敌");
//写入之后要刷新
osw.close();
//创建字符流读对象
InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\java2.txt"),"GBK");
//读
int ch;
while ((ch = isr.read()) != -1){
System.out.print((char) ch);
}
//关闭
isr.close();
}
}
运行结果:
字符流写数据的方法:
刷新和关闭方法:在写完数据后要进行刷新,操作完后要关闭流释放资源
示例代码:
public class CharStream_2 {
public static void main(String[] args) throws IOException {
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day09\\java3.txt"));
//void write(int c) 写一个字符
osw.write("48\r\n");
//void write(char[] cbuf) 写入一个字符数组
char[] ch = {'a','b','c'};
osw.write(ch);
osw.write("\r\n");
//void write(char[] cbuf, int off, int len) 写入字符数组的一部分
osw.write(ch,1,1);
osw.write("\r\n");
//void write(String str) 写一个字符串
String st = "ABCD";
osw.write(st);
osw.write("\r\n");
//void write(String str, int off, int len) 写一个字符串的一部分
osw.write(st,1,2);
osw.close();
}
}
运行结果:
字符流读取数据的方法:
实例代码:
public class CharStream_3 {
public static void main(String[] args) throws IOException {
InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\java3.txt"));
//int read() 一次读一个字符数据
/*int ch;
while ((ch = isr.read()) != -1){
System.out.print((char)ch);
}
isr.close();*/
//int read(char[] cbuf) 一次读一个字符数组数
char[] chars = new char[1024];
int len;
while ((len = isr.read(chars)) != -1){
System.out.print(new String(chars,0,len));
}
isr.close();
}
}
运行结果:
字符流复制文件:
实例代码:
public class CopyFile {
public static void main(String[] args) throws IOException {
//复制文件,边读边写
InputStreamReader isr = new InputStreamReader(new FileInputStream("day09\\java1.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("day09\\java5.txt"));
//复制
/*int ch;
while ((ch = isr.read()) != -1){
osw.write(ch);
}*/
char[] chars = new char[1024];
int len;
while ((len = isr.read(chars)) != -1){
osw.write(chars,0,len);
}
isr.close();
osw.close();
}
}
改进版:
public class CopyFileSuper {
public static void main(String[] args) throws IOException {
//创建字符流子类的对象,利用的是父类的方法
FileReader fileReader = new FileReader("day09\\java1.txt");
FileWriter fileWriter = new FileWriter("day09\\java6.txt");
/*int ch;
while ((ch = fileReader.read()) != -1){
fileWriter.write(ch);
}*/
char[] chars = new char[1024];
int len;
while ((len = fileReader.read(chars)) != -1){
fileWriter.write(chars,0,len);
}
fileReader.close();
fileWriter.close();
}
}
字符缓冲流:
字符缓冲流介绍 :
BufferedWriter:将文本写入字符输出流,缓冲字符,以提供单个字符,数组和字符串的高效写入,可 以指定缓冲区大小,或者可以接受默认大小。默认值足够大,可用于大多数用途
BufferedReader:从字符输入流读取文本,缓冲字符,以提供字符,数组和行的高效读取,可以指定缓 冲区大小,或者可以使用默认大小。 默认值足够大,可用于大多数用途
构造方法:
字符缓冲流特有功能:
实例代码:
public class CharBufferStream_2 {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\java8.txt"));
for (int i = 0; i < 5; i++) {
bw.write("hello");
//换行
bw.newLine();
}
bw.close();
BufferedReader br = new BufferedReader(new FileReader("day09\\java8.txt"));
String s;
while ((s = br.readLine()) != null){
System.out.println(s);
}
br.close();
}
}
运行结果:
字符缓冲流复制文件的两种方式:
示例代码:
public class CopyFileBuffer {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day09\\java1.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\java7.txt"));
//第一种
/*int ch;
while ((ch = br.read()) != -1){
bw.write(ch);
}*/
//第二种
char[] chars = new char[1024];
int len;
while ((len = br.read(chars)) != -1){
bw.write(chars,0,len);
}
br.close();
bw.close();
}
}
public class CopyFileBufferSuper {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("day09\\java8.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("day09\\java9.txt"));
//复制
String s;
while ((s = br.readLine()) != null){
bw.write(s);
bw.newLine();
}
br.close();
bw.close();
}
}