文件的输入输出流:
import java.io.*;public class Stream {//文件输出流public static void SerializableOblect(String path, Object o) throws IOException {FileOutputStream fos = null;ObjectOutputStream oos = null;fos = new FileOutputStream(path);oos = new ObjectOutputStream(fos);oos.writeObject(o);oos.close();fos.close();}//文件输入流public static Object reSerializableOblect(String repath) throws IOException, ClassNotFoundException {FileInputStream fis = null;ObjectInputStream ois = null;fis = new FileInputStream(repath);ois = new ObjectInputStream(fis);Object o = ois.readObject();ois.close();fis.close();return o;}
}
文件输入输出流:
输入流是数据流从硬盘向内存输出,输出流是数据流内存向硬盘输出
文件输出流的实现:
import java.io.IOException;public class Demo {public static void main(String[] args) {Supply supply = new Supply("food");try {Stream.SerializableOblect("文件路径",supply);} catch (IOException e) {e.printStackTrace();}}
}
文件输入流的实现:
import java.io.IOException;public class Demo {public static void main(String[] args) {try {Object o = Stream.reSerializableOblect("文件路径");Supply supply = (Supply) o;System.out.println(supply.getType());} catch (IOException e) {e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();}}
}