引言
我们在编写程序的时候发现,上一次的运行数据下次就用不了了。因为,这些运行数据是存在内存中的,程序运行结束就被清理了。如果想下一次再次使用的话就得存在磁盘上,之前刚学io的时候,我想到了一些馊主意——把数据用字节流存在文件里,然后下一次再读取。不过读取的数据需要自己去识别,过程肯定非常繁琐和困难。
在本周的专业课中,老师提到了一个对象流,把对象的数据打成二进制存进文件,就可以再次读取,而且读取的方式非常简单。
ObjectOutputStream
先写一个自定义类
import java.io.Serializable;
public class Test implements Serializable {
public static final long serialVersionUID= -5222904273650020694L;//该类的uid
private String string;
private double aDouble;
private int anInt;
public Test(String string, double aDouble, int anInt) {
this.string = string;
this.aDouble = aDouble;
this.anInt = anInt;
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public double getaDouble() {
return aDouble;
}
public void setaDouble(double aDouble) {
this.aDouble = aDouble;
}
public int getAnInt() {
return anInt;
}
public void setAnInt(int anInt) {
this.anInt = anInt;
}
@Override
public String toString() {
return "Test{" +
"string='" + string + '\'' +
", aDouble=" + aDouble +
", anInt=" + anInt +
'}';
}
}
一定要实现Serializable接口,这个接口相当于一个标记,标记这个类是可序列化的。serialVersionUID相当于这个类的身份证,无论以后这个类怎么变,它都能被识别出来。
import java.io.*;
public class Testmain {
public static void main(String[] args) {
Test test = new Test("测试", 19.8, 168);
String path=null;
FileOutputStream fileOutputStream=null;
ObjectOutputStream objectOutputStream=null;
try {
path="D:\\学习\\IdeaProjects\\java\\src\\test\\iotest\\iotest_2\\test.txt";//确定源
fileOutputStream=new FileOutputStream(path);//打开文件输出流
objectOutputStream=new ObjectOutputStream(fileOutputStream);//打开对象输出流
objectOutputStream.writeObject(test);//写入对象
objectOutputStream.flush();//刷新
}catch (IOException e){
System.out.println(e);
}finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();//关闭对象输出流
}
} catch (IOException e) {
System.out.println(e);
}
try {
if (fileOutputStream != null) {
fileOutputStream.close();//关闭文件输出流
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
然后我们可以把这个方法提取出来并封装。
import java.io.*;
public class Testmain {
public static void main(String[] args) {
Test test = new Test("测试", 19.8, 168);
String path="D:\\学习\\IdeaProjects\\java\\src\\test\\iotest\\iotest_2\\test.txt";//确定源
objOut(test,path);
}
private static void objOut(Object object,String path) {
FileOutputStream fileOutputStream=null;
ObjectOutputStream objectOutputStream=null;
try {
fileOutputStream=new FileOutputStream(path);//打开文件输出流
objectOutputStream=new ObjectOutputStream(fileOutputStream);//打开对象输出流
objectOutputStream.writeObject(object);//写入对象
objectOutputStream.flush();//刷新
}catch (IOException e){
System.out.println(e);
}finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();//关闭对象输出流
}
} catch (IOException e) {
System.out.println(e);
}
try {
if (fileOutputStream != null) {
fileOutputStream.close();//关闭文件输出流
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
ObjectInputStream
步骤和上面差不多,无非就是确定源,打开流,操作流,关闭流
package test.iotest.iotest_2;
import java.io.*;
public class Testmain {
public static void main(String[] args) {
Test test = new Test("测试", 19.8, 168);
String path="D:\\学习\\IdeaProjects\\java\\src\\test\\iotest\\iotest_2\\test.txt";//确定源
objOut(test,path);
FileInputStream fileInputStream=null;
ObjectInputStream objectInputStream=null;
try{
fileInputStream=new FileInputStream(path);
objectInputStream=new ObjectInputStream(fileInputStream);
Test test1=null;
test1=(Test) objectInputStream.readObject();
System.out.println(test1);
}catch (IOException | ClassNotFoundException e){
System.out.println(e);
}finally {
try {
if (objectInputStream != null) {
objectInputStream.close();
}
} catch (IOException e) {
System.out.println(e);
}
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
}
private static void objOut(Object object,String path) {
FileOutputStream fileOutputStream=null;
ObjectOutputStream objectOutputStream=null;
try {
fileOutputStream=new FileOutputStream(path);//打开文件输出流
objectOutputStream=new ObjectOutputStream(fileOutputStream);//打开对象输出流
objectOutputStream.writeObject(object);//写入对象
objectOutputStream.flush();//刷新
}catch (IOException e){
System.out.println(e);
}finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();//关闭对象输出流
}
} catch (IOException e) {
System.out.println(e);
}
try {
if (fileOutputStream != null) {
fileOutputStream.close();//关闭文件输出流
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
最后把两个方法都提取出来封装
package test.iotest.iotest_2;
import java.io.*;
public class Testmain {
public static void main(String[] args) {
Test test = new Test("测试", 17.8, 168);
String path="D:\\学习\\IdeaProjects\\java\\src\\test\\iotest\\iotest_2\\test.txt";//确定源
objOut(test,path);
Test test1=(Test) objIn(path);
System.out.println(test1);
}
private static Object objIn(String path) {
FileInputStream fileInputStream=null;
ObjectInputStream objectInputStream=null;
Object object=null;
try{
fileInputStream=new FileInputStream(path);
objectInputStream=new ObjectInputStream(fileInputStream);
object = objectInputStream.readObject();
}catch (IOException | ClassNotFoundException e){
System.out.println(e);
}finally {
try {
if (objectInputStream != null) {
objectInputStream.close();
}
} catch (IOException e) {
System.out.println(e);
}
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
System.out.println(e);
}
}
return object;
}
private static void objOut(Object object,String path) {
FileOutputStream fileOutputStream=null;
ObjectOutputStream objectOutputStream=null;
try {
fileOutputStream=new FileOutputStream(path);//打开文件输出流
objectOutputStream=new ObjectOutputStream(fileOutputStream);//打开对象输出流
objectOutputStream.writeObject(object);//写入对象
objectOutputStream.flush();//刷新
}catch (IOException e){
System.out.println(e);
}finally {
try {
if (objectOutputStream != null) {
objectOutputStream.close();//关闭对象输出流
}
} catch (IOException e) {
System.out.println(e);
}
try {
if (fileOutputStream != null) {
fileOutputStream.close();//关闭文件输出流
}
} catch (IOException e) {
System.out.println(e);
}
}
}
}
瑞思拜!