标签:
接口:Serializable 功能:启动序列化
简介:类实现了这个接口就会是吸纳序列化;
功能描述:什么是序列化?就是把我们的对象转化为可存储的字节流,通过反序列化来付支出相同的对象;你可以想想RMI的实现,是如何传输对象信息的;
简单实现:
序列化:
package com.dom.Serializable; import java.io.Serializable; public class Human implements Serializable { private static String sss="sss"; private String name; private int age; public void speak(){ System.out.println("i want to talk to you!"); } }
package com.dom.Serializable; public class Men extends Human { private Integer sex; @Override public void speak() { super.speak(); System.out.println("i am a men!"); } }
package com.dom.Serializable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SimpleSerial { /** * 序列化和反序列化的实现类 * * @author Administrator * * **/ public static void main(String[] args) { //创建一个File对象 File file = new File("f://human.file"); try { //把对象传入这个file ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); Human human = new Human(); human.setAge(23); human.setName("admin"); oos.writeObject(human); oos.close(); //反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); Human newHuman = (Human)ois.readObject(); System.out.println("姓名:"+newHuman.getName()); System.out.println("年级:"+newHuman.getAge()); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
运行结果:姓名:admin 年级:23
如果我们把Human的接口实现去除运行就会报错:
package com.dom.Serializable; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SimpleSerial { /** * 序列化和反序列化的实现类 * * @author Administrator * * **/ public static void main(String[] args) { //创建一个File对象 File file = new File("f://human.file"); try { //把对象传入这个file ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file)); Human human = new Men(); human.setAge(23); human.setName("admin"); oos.writeObject(human); oos.close(); //反序列化 ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file)); Human newHuman = (Human)ois.readObject(); System.out.println("姓名:"+newHuman.getName()); System.out.println("年级:"+newHuman.getAge()); newHuman.speak(); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } } }
我们把humen对象变为men
输出结果:姓名:admin 年级:23 i want to talk to you! i am a men!
注:父类实现序列化 子类继承 ;序列化用于传输对象状态;
类:File
简介:文件和目录路径名的抽象表示形式。
功能介绍:我们可以在本地 网络服务器上创建文件
简单实现:
package com.dom.file; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileDom { public static void main (String [] ags){ try { File file = new File("F:\\admin.txt"); //文件创建 file.createNewFile(); //文件读取 InputStream is = new FileInputStream(file); //文件复制 File file1 = new File("F:\\admin1.txt"); OutputStream os = new FileOutputStream(file1); byte[] buffer = new byte[400]; int length = 0; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } is.close(); os.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
至于图片读写 reader writer buffer都是同一个道理;运用差不多
标签:
原文地址:http://my.oschina.net/u/1249631/blog/394811