1.IO流
从磁盘读取数据到内存(Java代码)====》java代码(输出出来)
输入流:
input :场景使用 从磁盘的c:/aaa/1.txt文件中 读取数据 到内存中(Java代码)
字节输入流 FileInputStream Read()
字符输入流 FileReader Read()
从内存(Java代码 String="狗蛋")写入数据到 磁盘(c盘 1.txt)
输出流:
output : 从内存(String str = " 还是数据库的借口借口") 写入到磁盘的c:/aaa/1.txt文件中
字节输出流: FileOutputStream Write()
字符输出流 FileWrite Write()
参照物体 是内存
图片 音频 视频 发现 字符流不能操作 这些。只能使用字节流操作
2,缓存区
输入流:
input :场景使用 从磁盘的c:/aaa/1.txt文件中 读取数据 到内存中(Java代码)
字节输入流 BufferedInputStream
字符输入流 BufferedReader
从内存(Java代码 String="狗蛋")写入数据到 磁盘(c盘 1.txt)
输出流:
output : 从内存(String str = " 还是数据库的借口借口") 写入到磁盘的c:/aaa/1.txt文件中
字节输出流: BufferedOutputStream
字符输出流 BufferedWrite
public class Demo1 {
public static void main(String[] args) throws IOException {
//字符流将一个文本赋值2到另外一个文件夹下面
//BufferedReader 专门将文本内容读取到内存中
BufferedReader br = new BufferedReader(new FileReader("c:/bbb/《雪中悍刀行》.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("c:/aaa/88.txt"));
char[] cbuf= new char[1024];
int length;
while ((length = br.read(cbuf)) != -1) {
bw.write(cbuf, 0, length);
}
bw.close();
br.close();
}
}
3,序列化和反序列化
新建类然后创建对象,然后对对象的二属性赋值。对象真实存在了,然后可以通过流来将对象存到本地磁盘上面,这就叫序列化。 本次磁盘上面存的有对象的数据,咱们可以再通过流读取到Java代码中变成对象这叫反序列化 持久性操作
注意事项: 请注意,一个类的对象要想序列化成功,必须满足两个条件:
该类必须实现 java.io.Serializable 接口。
该类的所有属性必须是可序列化的。如果有一个属性不是可序列化的,则该属性必须注明是短暂的
3.1从内存到磁盘 序列化
输出流 ObjectOutputStream write
class Employee implements Serializable {
String name;
int age;
transient int ID;//短暂的 此属性不可序列化
String adress;//地址
public void eat() {
System.out.println("今天中午没有吃饱");
}
}
public class Demo1 {
public static void main(String[] args) throws IOException {
ArrayList arrayList = new ArrayList();
Employee employee = new Employee();
employee.name = "gousheng";
employee.adress = "航海中路";
employee.age = 18;
employee.ID = 8989;
//将Java代码中的对象存到磁盘中
FileOutputStream fos = new FileOutputStream("c:/aaa/emp.ser");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(employee);
oos.close();
fos.close();
}
}
3.2,反序列化
从磁盘到 内存中 inputStream
public class Demo2 {
public static void main(String[] args) throws Exception {
//反序列化
FileInputStream fis = new FileInputStream("c:/aaa/emp.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Employee emp = (Employee)ois.readObject();
System.out.println(emp.name);
System.out.println(emp.age);
System.out.println(emp.adress);
System.out.println(emp.ID);//0
}
}