IO流
我们都漂泊在茫茫大海上,没有救生圈、救生网,却以为自己就是别人的拯救者。
在学习过程中持续修改…
[ 注:以下所有代码省略了异常处理 ]
文章目录
一、简介
1. 流的分类
-
数据单位:字节流(8bit)、字符流(16bit)
-
流向:输入流、输出流
-
角色:节点流、处理流
抽象基类 字节流 字符流
输入流 InputStream Reader
输出流 OutputStream Writer
2. IO流体系
分类 | 字节输入流 | 字节输出流 | 字符输入流 | 字符输出流 |
---|---|---|---|---|
抽象基类 | InputStream | OutputStream | Reader | Writer |
访问文件 | FileInputStream | FileOutputStream | FileReader | FileWriter |
访问数组 | ByteArrayInputStream | ByteArrayOutputStream | CharArrayReader | CharArrayWriter |
访问管理 | PipedInputStream | PipedOutputStream | PipedReader | PipedWriter |
访问字符串 | StringReader | StringWriter | ||
缓冲流 | BufferedInputStream | BufferedOutputStream | BufferedReader | BufferedWriter |
转换流 | InputStreamReader | OutputStreamWriter | ||
对象流 | ObjectInputStream | ObjectOutputStream | ||
FilterInputStream | FilterOutputStream | FilterReader | FilterWriter | |
打印流 | PrintStream | |||
推回输入流 | PushbackInputStream | PushbackReader | ||
特殊流 | DataInputStream | DateOutputStream |
二、FileReader和FileWriter
1. 简介
- FileReader和FileWriter用来处理文本文件,无法处理非文本文件
- 输入的文件必须存在,否则出现异常
- 输出的文件可以不存在,若不存在则创建
2. 常用方法
- 1)构造方法:
- FileReader(File)
- FileReader(File, Charset)
- FileWriter(File)
- FileWriter(File,boolean)
- FileWriter(File, Charset)
- FileWriter(File, Charset, boolean)
- 2)读取
- read():int,返回值为读取的一个字符的ACSII码,文件末尾为-1
- read(char[]):int,返回值为读取字符个数,文件末尾为-1
- 3)写入
- write(int)
- write(char[], int, int)
- write(String)
3. 关键代码
-
1)读取:
-
FileReader fr = null; //1.实例化File类的对象,指明要操作的文件 File file = new File("src/hello.txt"); //2.提供具体的流 fr = new FileReader(file); //3,数据的读入 //read()返回读入的一个字符,如果达到文件末尾,返回-1 //方式一: // int data = fr.read(); // while(data != -1){ // System.out.print((char) data); // data = fr.read(); // } //方式二 // int data; // while((data = fr.read()) != -1){ // System.out.print((char) data); // } //方式三 char[] cbuf = new char[5]; int len; while ( (len = fr.read(cbuf)) != -1){ //方式一: // for (int i=0; i<len; i++){ // System.out.print(cbuf[i]); // } //方式二 String str = new String(cbuf,0, len); System.out.print(str); }
-
-
2)读取、写入同时进行:
-
//1.创建File对象,指明读、写文件 File srcFile = new File("src/hello.txt"); File destFile = new File("src/hello3.txt"); //2.创建输入流、输出流 FileReader fr = null; FileWriter fw = null; fr = new FileReader(srcFile); fw = new FileWriter(destFile); //3.读入、写出 char[] cbuf = new char[10]; int len; while ((len = fr.read(cbuf)) != -1){ String temp = new String(cbuf, 0, len); System.out.println(temp); // fw.write(cbuf, 0, len); fw.write(temp); }
-
三、FileInputStream和FileOutputStream
1. 简介
- FileInputStream和FileOutputStream处理非文本文件,若处理文本文件可能出现乱码(在读取过程中查看数据时)
- 若复制文本文件,没有问题
- utf-8一个中文字符占3个字节
2. 常用方法
- 1)构造方法:略
- 2)读取
- read():int
- read(byte[]):int,返回值为读取字节数
- 3)写入
- write(int)
- write(byte[], int, int)
3. 关键代码
-
1)读、写
-
//1.实例化文件 File srcFile = new File("src/1.jpg"); File destFile = new File("src/1_copy.jpg"); //2.创造流 FileInputStream fis = null; FileOutputStream fos = null; fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); //3.复制过程 byte[] buffer = new byte[1024]; int len; while((len = fis.read(buffer)) != -1){ System.out.println("len:" + len); fos.write(buffer, 0, len); } //4.关闭流
-
四、缓冲流
1. 简介
-
缓冲流是处理流的一种
-
1)四个缓冲流:
BufferedInputStream BufferedOutputStream BufferedReader BufferedWriter
-
2)缓冲流的功能:提高读、写速度
-
3)能够提高读写速度的原因:内部提供了一个缓冲区
-
private static int DEFAULT_BUFFER_SIZE = 8192;
-
-
4)关闭流时,在关闭外层流的同时,内层流也会自动的关闭。故在使用处理流时,只需要关闭处理流即可。
2. 常用方法
-
1)read()、write()方法与FileInputStream、FileOutputStream中的一致,不做阐述。
-
2)不同之处:
-
readLine():读取一行,返回值为字符串。读取数据不包含换行符。
-
BufferedReader br = new Buffered(new FileReader(new File("..."))); String data; while((data = br.readLine()) != null){ bw.write(data); //data中不包含换行符。 bw.newLine(); //添加换行符 }
-
3. 关键代码
//1.文件
File srcFile = new File("src/1.jpg");
File destFile = new File("src/1._buff_copy.jpg");
//2.流
//2.1字节流
FileInputStream fis = null;
FileOutputStream fos = null;
//2.2缓冲流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
//3.复制过程
byte[] buffer = new byte[1024];
int len;
while((len=bis.read(buffer)) != -1){
bos.write(buffer, 0, len);
// bos.flush(); //刷新缓冲区
}
五、转换流
1. 简介
- 转换流是处理流的一种
- 作用:提供了字节流和字符流之间的转换
- InpuStreamReader和OutputStreamWriter
- 解码:字节、字节数组 —> 字符数组、字符串
- 编码:字符数组、字符串 —> 字节、字节数组
- 编码:编译为计算机语言(字节)
- 解码:解析成人类语言(字符)
- 注意:转换流需要指定以什么字符集读写数据(取决于文件存储时的字符集),默认为系统字符集。
2. 常用方法
- 1)构造方法:注意指定以何种字符集读取字符
- InputStreamReader(InputStream)
- InputStreamReader(InputStream, Charset)
- OutputStreamWriter(OutputStream)
- OutputStreamWriter(OutputStream, Charset)
- 2)读写与Reader、Writer一致
3. 关键代码
//1.指明文件
File file1 = new File("src/hello.txt");
File file2 = new File("src/hello_gbk.txt");
//2.创造流
FileInputStream fis = null;
FileOutputStream fos = null;
InputStreamReader isr = null;
OutputStreamWriter osw = null;
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis, "utf-8");
osw = new OutputStreamWriter(fos, "gbk");
//3.操作文件
char[] buffer = new char[10];
int len;
while ((len = isr.read(buffer)) != -1){
osw.write(buffer, 0, len);
}
//4.关闭流
4. 字符集
字符集的演变:0和1 —> ASCII —> ANSI —>Unicode,
ANSI包括:GB2312、BIC5、JIS、GBK…
Unicode包括:UTF-8、UTF-16…
- ASCII:美国标准信息交换码,一个字节的7位可表示
- IOS8859-1:拉丁码表,欧洲码表,一个字节的8位表示
- GB2312:中国的中文编码表,最多两个字节编码所有字符
- GBK:中国的中文编码表升级,融合了更多的中文文字符号,最多两个字节编码
- Unicode:国际标准码,融合了目前人类使用的所有字符,为每个字符分配唯一的字符码,所有的文字都用两个字节表示。Unicode字符集只是定义了字符集合和唯一编号,是对UTF-8、UTF-16/UCS-2等具体编码方案的统称,而不是具体的方案。
- UTF-8:边长的编码方式,可用1-4个字节来表示一个字符(一个汉字三个字节)。每次8个位传输数据。
- UTF-16:每次16个位传输数据
六、标准输入输出流
1. 简介
- System.in:标准输入流,默认从控制台输入
- System.out:标准输出流,默认从控制台输出
- 可以通过System的setIn()和setOut()方法,重新指定输入、输出的位置
2. 关键代码
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
while (true) {
String data = br.readLine();
if ("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)){
System.out.println("程序结束!");
break;
}
System.out.println(data.toUpperCase());
七、打印流
1. 简介
- PrintStream
- PrintWriter
- 打印流也是处理流的一种
2. 关键代码
//修改System.out.println()的输出方向
PrintStream ps = null;
FileOutputStream fos = new FileOutputStream(new File("src/printStream.txt"));
ps = new PrintStream(fos, true);
if(ps != null){
System.setOut(ps);
}
for (int i = 0; i<=255; i++){
System.out.print((char)i);
if(i % 50 == 0){
System.out.println();
}
}
八、数据流
1. 简介
- DataInputStream and DataOutputStream
- 方便操作JAVA语言的基本数据类型和String的数据
- 数据流也是处理流的一种
- 读取数据顺序与写入顺序必须一致
2. 方法
- 1)DataInputStream中的方法
- boolean readBoolean()
- byte readByte()
- char readChar()
- float readFloat()
- double readDouble()
- short readShort()
- long readLong()
- int readInt()
- String readUTF()
- void readFully(byte[] b)
八、数据流
1. 简介
- DataInputStream and DataOutputStream
- 方便操作JAVA语言的基本数据类型和String的数据
- 数据流也是处理流的一种
- 读取数据顺序与写入顺序必须一致
2. 方法
- 1)DataInputStream中的方法
- boolean readBoolean()
- byte readByte()
- char readChar()
- float readFloat()
- double readDouble()
- short readShort()
- long readLong()
- int readInt()
- String readUTF()
- void readFully(byte[] b)
- 2)DataOutputStream中的方法:将上述的read改为write