JAVA I/O框架(一)
一. 流的概念
概念:内存与存储设备之间传输数据的通道
二. 流的分类
按方向【重点】
- 输入流:将<存储设备>中的内容读入到<内存>中
- 输出流:将<内存>中的内容写入到<存储设备>中
按单位
- 字节流:以字节为单位,可以读写所有数据。
- 字符流:以字符为单位,只能读写文本数据。
按功能
- 节点流:具有实际传输数据的读写功能。
- 过滤流:在节点流的基础之上增强功能。
三. 字节流
字节流的父类(抽象类):
InputStream :字节输入流
public int read() {}
public int read (byte[] b){}
public int read (byte[] b, int off,int len){}
OutputStream :字节输出流
public void write(int n){}
public void write (byte[] b){}
public void write(byte[] b,int off,int len){}
1. 文件字节流
- FileInputStream:
public int read(byte[] b) //从流中读取多个字节,将读到内容存入b数组,返回实际读到的字节数;如果达到文件的尾部,则返回-1
文件字节输入流实例:
import java.io.FileInputStream;
//文件字节输入流
public class Application {
public static void main(String[] args) throws Exception{
//创建FileInputStream,并指定文件路径
FileInputStream fis = new FileInputStream("D:\\a.txt");
//读取文件
int data=0;
//第一种,按单个字节读取
// while ((data= fis.read())!=-1){
// System.out.println((char) data);
// }
//第二种,按多个字节读取
byte[] buf = new byte[3];
int count=0;
while ((count=fis.read(buf))!=-1){
System.out.println(new String(buf,0,count));
}
fis.close();
System.out.println("执行完毕!");
}
}
---------------结果---------------
ABC
DEF
G
执行完毕!
-------------文件内容-------------
ABCDEFG
- FileOutputStream:
public void write(byte[] b)//一次写多个字节,将b数组中所有字节,写入输出流。
文件字节输出流实例:
import java.io.FileOutputStream;
//文件字节输出流
public class Application {
public static void main(String[] args) throws Exception{
//创建FileOutputStream,并指定文件路径
FileOutputStream fos = new FileOutputStream("D://b.txt");
//FileOutputStream fos = new FileOutputStream("D://b.txt",true);//不覆盖原文件
//写入文件
String str="Hello World";
fos.write(str.getBytes());
//关闭
fos.close();
System.out.println("程序结束!");
}
}
---------------结果---------------
程序结束!
-------------文件内容-------------
Hello World
文件复制实例:
import java.io.FileInputStream;
import java.io.FileOutputStream;
//文件复制
public class Application {
public static void main(String[] args) throws Exception{
//文件字节输入流
FileInputStream fis = new FileInputStream("D://001.jpg");
//文件字节输出流
FileOutputStream fos = new FileOutputStream("D://002.jpg");
byte[] byt= new byte[1024];
int count=0;
while ((count = fis.read(byt))!=-1){
fos.write(byt,0,count);
}
fis.close();
fos.close();
System.out.println("执行完毕!");
}
}
---------------结果---------------
执行完毕!
2. 字节缓冲流
- 缓冲流: BufferedInputStream/ BufferedOutputStream
提高I/O效率,减少访问磁盘的次数;
数据存储在缓冲区中, flush是将缓存区的内容写入文件中,也可以直接 close
字节缓冲流读取实例:
import java.io.BufferedInputStream;
import java.io.FileInputStream;
//字节缓冲流读取
public class Application {
public static void main(String[] args) throws Exception{
//创建BufferedInputStream
FileInputStream fis = new FileInputStream("D://a.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
//读取 用bis的缓冲区 8K
// int data=0;
// while ((data=bis.read())!=-1){
// System.out.print((char)data);
// }
//用自己设置的缓冲区
byte[] byt = new byte[1024];
int count=0;
while ((count=bis.read(byt))!=-1){
System.out.println(new String(byt,0,count));
}
//关闭
fis.close();
}
}
---------------结果---------------
ABCDEFG
字节缓冲流输出实例:
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
//字节缓冲流写入
public class Application {
public static void main(String[] args) throws Exception{
//BufferedOutputStream
FileOutputStream fos = new FileOutputStream("D://b.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
//写入文件
for (int i = 0; i < 10; i++) {
bos.write("Hello World\r\n".getBytes());//缓冲区8K
bos.flush();//刷新到硬盘
}
bos.close();//内部调用flush()方法
System.out.println("执行结束!");
}
}
---------------结果---------------
执行结束!
-------------文件内容-------------
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
3. 对象流
对象流:ObjectOutputStream/ ObjectInputStream
-
增强了缓冲区功能
-
增强了读写8种基本数据类型和字符串功能
-
增强了读写对象的功能
readObject() 从流中读取一个对象 writeObject(Object obj )向流中写入一个对象
使用流传输对象的过程称为序列化、反序列化。
序列化实例:
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class Application {
public static void main(String[] args) throws Exception{
//创建对象流
FileOutputStream fos = new FileOutputStream("D://stu.bin");
ObjectOutputStream oos = new ObjectOutputStream(fos);
//序列化(写入操作)
Student s1 = new Student("张三", 20);
Student s2 = new Student("李四", 20);
Student s3 = new Student("王五", 20);
oos.writeObject(s1);
oos.writeObject(s2);
oos.writeObject(s3);
//关闭
oos.close();
System.out.println("执行结束!");
}
}
---------------结果---------------
执行结束!
------------文件内容--------------
sr com.mnm.javaSe.ioFrame.Student摌?!e&? I ageL namet Ljava/lang/String;xp t 寮犱笁sq ~ t 鏉庡洓sq ~ t 鐜嬩簲
//乱码
这个对象想要序列化,这个类必须先实现Serializable接口
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public Student() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student[" +
"name='" + name +
", age=" + age +
']';
}
}
反序列化实例:
import java.io.FileInputStream;
import java.io.ObjectInputStream;
public class Application {
public static void main(String[] args) throws Exception{
//创建对象流
FileInputStream fis = new FileInputStream("D://stu.bin");
ObjectInputStream ois = new ObjectInputStream(fis);
//读取文件(反序列化)
Student s1 = (Student) ois.readObject();
Student s2 = (Student) ois.readObject();
Student s3 = (Student) ois.readObject();
//关闭
ois.close();
System.out.println("执行结束");
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
---------------结果---------------
执行结束
Student[name='张三, age=20]
Student[name='李四, age=20]
Student[name='王五, age=20]
注意事项:
(1)序列化类必须要实现Serializable接口
(2)序列化类中对象属性要求实现Serializable接口
(3)序列化版本号ID,保证序列化的类和反序列化的类是同一个类
(4)使用 transient(瞬间的)修饰属性,这个属性不能序列化
(5)静态属性也不能序列化
(6)序列化多个对象的时候,可以借助集合来实现
四. 编码方式
ISO-8859-1收录除ASCII外,还包括西欧、希腊语、泰语、阿拉伯语、希伯来语对应的文字符号
- UTF-8–>针对 Unicode码表的可变长度字符编码
- GB2312–>简体中文
- GBK–>简体中文、扩充
- BIG5–>台湾,繁体中文
当编码方式和解码方式不一致时,会出现乱码。
下一篇
JAVA I/O框架(二)
链接