IO流
一 流的概念
内存与存储设备之间传输数据的通道
注意:站在程序的角度
I:In,输入流,read读取(读取文件中的数据)
O:Out,输出流,writer写入(向文件里写入数据)
流:一点点的读取或写入
二 流的分类
1 按方向【重点】:
2 按单位:
注意:中文在UTF-8中一个字占3个单位,在GBK中一个字占2个单位
3 按功能:
1024位单位进制,KB》MB》GB》TB》PB
注意:流与流之间的继承关系
三 字节流
1字节流的父类(抽象类)
2.字节节点流
-
FileOutputStream:
- public void write(byte[] b)//依次写多个字节,将b数组中所有字节,写入输出流
知识点:FileInputStream 文件字节输入流
需求:利用文件字节输入流读取文件中的数据
1)文件存在的情况,读取一个字节FileInputStream fileInputStream=new FileInputStream("io.txt"); System.out.println(fileInputStream.read());//读取数据,读取第一个字节,返回ASCII嘛 fileInputStream.close();
需求:利用文件字节输入流读取文件中的数据
1)文件存在的情况,读取一个字节
2)文件不存在的情况,读取一个字节(经验:所有的输入流,文件不存在就报错)
3)一个一个字节的读取整个文件FileInputStream f=new FileInputStream("io.txt"); int read; //读取数据 read()依次读取单个字节,当读取到文件末尾时返回-1 while((read=f.read())!=-1){ System.out.println((char)read); } f.close();
知识点:FileInputStream 文件字节输入流
需求:利用文件字节输入流读取文件中的数据
1)文件存在的情况,读取一个字节
2)文件不存在的情况,读取一个字节(经验:所有的输入流,文件不存在就报错)
3)一个一个字节的读取整个文件
4)1024个1204个字节的读取整个文件FileInputStream f=new FileInputStream("io.txt");//创建对象 byte[] b=new byte[1024]; int len; while((len=f.read(b))!=-1){ System.out.println(b,0,len); }//以1024来读取数据 f.close();//关流
-
FileInputStream:
- public void read(byte[] b)//从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1
知识点:FileOutputStream 文件字节输出流
需求:利用文件字节输出流向文件写入数据
1) 文件存在的情况
FileOutputStream f=new FileOutputStream("io.txt");
//写入数据的方法:
f.write(97);//ASCII码
f.write("123abc".getBytes());//写入字节数组
f.write("123adb".getBytes(),2,3)//写入字节数组,偏移量,写入字节
f.close();//关流
知识点:FileOutputStream 文件字节输出流
需求:利用文件字节输出流向文件写入数据
1) 文件存在的情况
2) 文件不存在的情况(经验:所有的输出流,当文件不存在时自动创建文件)
3) 向文件末尾追加内容
FileOutputStream f=new FileOutputStream("io.txt",true);
f.write("123abc".getBytes());
f.close();
3.字节过滤流
-
缓存流:BufferdOutputStream/BufferedInputStream
-
提高IO流效率,减少了访问磁盘的次数
-
数据存储在缓冲区中,flush是将缓存区的内容写入文件中,也可以是close,因为在源码中,close方法包含了flush的用法。源码如下:
-
public void close() throws IOException { try (OutputStream ostream = out) { flush(); } } public void flush() throws IOException { out.flush(); }
-
-
知识点:BufferedOutputStream 带有缓冲区的字节输出流
需求:利用带有缓冲区的字节输出流向文件写入数据
//使用默认的缓冲区大小:8192字节
BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream("io.txt"));
//自定义缓冲区长度大小:
BufferedOutputStream bos1=new BufferedOutputStream(new FileOutputStream("io.txt"),10000);
//在文件末尾追加数据
BufferedOutputStream bos2=new BufferedOutputStream(new FileOutputStream("io.txt",true));
bos2.write("123abc".getBytes());
bos2.close();
知识点:BufferedInputStream 带有缓冲区的字节输入流
需求:利用带有缓冲区的字节输入流读取文件里的数据
BufferedInputStream bis=new BufferedInputStream(new FileInputStream("io.txt"));
byte[] b=new byte[1024];
int len;
while((len=bis.read(b))!=-1){
System.out.println(new String (b,0,len));
}
bis.close();
Copy方法:
BufferedInputStream bis=null;
BufferedOutputStream bos=null;
try{
bis=new BufferedInputStream(new FileInputStream("io.txt"));
bos=new BufferedOutputStream(new FileOutputStream("copy.txt"));
byte[] b=new byte[1024];
int len ;
while((len=bis.read(b))!=-1){
bos.write(b,0,len)
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(bis!=null){
try{
bis.close();
}catch(IOException e){
e.printStackTrace();
}
}
if(bos!=null){
try{
bos.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
- 对象流:ObjectOutputStream/ObjectInputStream
- 增强了缓冲区功能
- 增强了读写8种基本数据类型和字符串功能
- 增强了读写对象的功能:
- readObject()从流中读取一个对象
- writeObject(Object obj)向流中写入一个对象
注意:使用流传输对象的过程被称之为序列化、反序列化
-
对象序列化的细节:
- 必须实现Serializable的接口
- 必须保证所有属性都可以被序列化
- transient修饰符为临时属性,不参与序列化,被修饰的时候,输出该属性时将被隐藏起来
- 读取到文件尾部的标志:java.io.EOFException
四 编码方法
- ISO-8859-1: 收录除ASCII码外,还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字字符
- UTF-8:针对Unicode的可变长度字符编码
- GB2312: 简体中文
- GBK:简体中文、扩充
- BIG5:台湾,繁体中文
- 注意:在编码的时候,一定要将编码方式和解码方式写为一致,否则会出现乱码的情况
五 字符流
1 字符流的父类(抽象类):
2 字符节点流:
- FileWriter:
- public void writer(String str)//依次写多个字符,将b数组中所有字符,写入输出流
- FileReader:
- public int read(char[] c)//从流中读取多个字符,将读到内容存入c数组中,返回实际的字符数;如果到文件的尾部,则返回-1。
3 字符过滤流:
- 缓冲流:BufferedWriter/BufferedReader:
- 支持换行符
- 可以依次写一行读一行
- 与其他流不同,在读取到文件尾部的时候,返回null
- PrintWriter:
- 封装了print()/println()方法,支持写入后换行。
- 支持数据原样打印
4. 字符节点流
- 桥转换流:InputStreamReader/OutputStreamWriter
- 可以将字节流转换为字符流
- 一定要设置字符的编码方式,保持一致,编码乱码的问题出现
5. 使用流的步骤
- 创建节点流
- [创建过滤流 设置字符编码]
- 封装过滤流
- 读写数据
- 关流
- 封装了print()/println()方法,支持写入后换行。
- 支持数据原样打印
4. 字符节点流
- 桥转换流:InputStreamReader/OutputStreamWriter
- 可以将字节流转换为字符流
- 一定要设置字符的编码方式,保持一致,编码乱码的问题出现
5. 使用流的步骤
- 创建节点流
- [创建过滤流 设置字符编码]
- 封装过滤流
- 读写数据
- 关流