字符流出现的原因和形式
由于字节流操作中文时,会将一个中文根据不同的编码表转化为不同的字节来读
而字符流遇到中文时会根据一个一个中文来读(以中文为单位)
编码表:
Unicode:国际标准码,融合了多种文字。
所有文字都用两个字节来表示,Java语言使用的就是unicode
UTF-8:最多用三个字节来表示一个字符。
UTF-8不同,它定义了一种“区间规则”,这种规则可以和ASCII编码保持最大程度的兼容:
它将Unicode编码为00000000-0000007F的字符,用单个字节来表示
它将Unicode编码为00000080-000007FF的字符用两个字节表示
它将Unicode编码为00000800-0000FFFF的字符用3字节表示
字符流=字节流+编码表。
三大类字符流
InputStreamReaderAndWriter
InputStreamReader的方法
int read():一次读取一个字符
int read(char[] chs):一次读取一个字符数组
InputStreamReader re = new InputStreamReader(new FileInputStream(
"a.txt"));
// 一次读取一个字符
// int ch = 0;
// while ((ch = re.read()) != -1) {
// System.out.print((char) ch);
// }
// 一次读取一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = re.read(chs)) != -1) {
System.out.print(new String(chs, 0, len));
}
// 释放资源
isr.close();
OutputStreamWriter的方法
public void write(int c):写一个字符
public void write(char[] cbuf,int off,int len):写一个字符数组的一部分
// 封装数据源
InputStreamReader isr = new InputStreamReader(new FileInputStream(
"a.txt"));
// 封装目的地
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream(
"b.txt"));
// 读写数据
// 方式1
// int ch = 0;
// while ((ch = isr.read()) != -1) {
// osw.write(ch);
// }
// 方式2
char[] chs = new char[1024];
int len = 0;
while ((len = isr.read(chs)) != -1) {
osw.write(chs, 0, len);
// osw.flush();
}
// 释放资源
osw.close();
isr.close();
FileWriterAndReader:
其实文件字符流的读写方法与InputStreamReaderAndWriter方法一样只是在构造方法中之间可以加文件或者文件路径。
// 封装数据源
FileReader fr = new FileReader("a.txt");
// 封装目的地
FileWriter fw = new FileWriter("b.txt");
// 一次一个字符
// int ch = 0;
// while ((ch = fr.read()) != -1) {
// fw.write(ch);
// }
// 一次一个字符数组
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
fw.flush();
}
// 释放资源
fw.close();
fr.close();
BufferReaderAndWriter
BufferReader:从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
public String readLine() :一次读取一行
BufferWriter:将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
public void write(String str):一次写一行
public void newLine():加入换行符
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("g.txt")));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("g5.txt")));
String d;
while((d = reader.readLine())!=null){
write.write(d);
write.newLine();
}
reader.close();
write.close();
面试题
close和flush的区别
close():关闭流对象之前会先刷新缓冲区,关闭之后,流对象不能使用
flush():仅仅刷新缓冲区,刷新之后流对象还可以继续使用
一般只要close
数据大 数据达到一定值时 flush再close 不是每次都要flush
数据少 close
啥叫转换流
转换流是指将字节流转换为字符流
InputStreamReader(InputStream in)
FileReader(File file) -->不是转换流
字符流和字节流的最重要的九种输入输出方式
package day21;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
//将a.txt复制到f.txt
public class ByteAndChar总结Demo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File f = new File("a.txt");
//字节流
mehtod(f);
mehtod1(f);
mehtod2(f);
mehtod3(f);
//字符流
gg(f);
gg1(f);
gg2(f);
gg3(f);
gg4(f);
}
private static void gg4(File f) throws IOException {
// TODO Auto-generated method stub
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
BufferedWriter write = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("g5.txt")));
String d;
while((d = reader.readLine())!=null){
write.write(d);
write.newLine();
}
reader.close();
write.close();
}
private static void gg3(File f) throws IOException {
// TODO Auto-generated method stub
InputStreamReader reader = new InputStreamReader(new FileInputStream(f));
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream("g.txt"));
int len;
while((len = reader.read())!=-1){
write.write(len);
write.flush();
}
reader.close();
write.close();
}
private static void gg2(File f) throws IOException {
// TODO Auto-generated method stub
InputStreamReader reader = new InputStreamReader(new FileInputStream(f));
OutputStreamWriter write = new OutputStreamWriter(new FileOutputStream("g1.txt"));
int len;
char[] ch = new char[1024];
while((len = reader.read(ch))!=-1){
write.write(ch,0,len);
write.flush();
}
reader.close();
write.close();
}
private static void gg1(File f) throws IOException {
// TODO Auto-generated method stub
FileReader reader = new FileReader(f);
FileWriter write = new FileWriter("g2.txt");
int len;
while((len = reader.read())!=-1){
write.write(len);
write.flush();
}
reader.close();
write.close();
}
private static void gg(File f) throws IOException {
// TODO Auto-generated method stub
FileReader reader = new FileReader(f);
FileWriter write = new FileWriter("g3.txt");
int len;
char[] ch = new char[1024];
while((len = reader.read(ch))!=-1){
write.write(ch,0,len);
write.flush();
}
reader.close();
write.close();
}
private static void mehtod3(File f) throws IOException {
// TODO Auto-generated method stub
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bou = new BufferedOutputStream(new FileOutputStream("f3.txt"));
int len;
while((len = bin.read())!=-1){
bou.write(len);
}
bin.close();
bou.close();
}
private static void mehtod2(File f) throws IOException {
// TODO Auto-generated method stub
BufferedInputStream bin = new BufferedInputStream(new FileInputStream(f));
BufferedOutputStream bou = new BufferedOutputStream(new FileOutputStream("f2.txt"));
int len;
byte[] byt = new byte[1024];
while((len = bin.read(byt))!=-1){
bou.write(byt,0,len);
}
bin.close();
bou.close();
}
private static void mehtod1(File f) throws IOException{
// TODO Auto-generated method stub
FileInputStream in = new FileInputStream(f);
FileOutputStream out = new FileOutputStream("f1.txt");
int len;
byte[] byt = new byte[1024];
while((len = in.read(byt))!=-1){
out.write(byt,0,len);
}
in.close();
out.close();
}
private static void mehtod(File f) throws IOException {
// TODO Auto-generated method stub
FileInputStream in = new FileInputStream(f);
FileOutputStream out = new FileOutputStream("f.txt");
int len;
while((len = in.read())!=-1){
out.write(len);
}
in.close();
out.close();
}
}