Java学习记录(补充十:序列化以及异常处理)

Student类
package com.jredu.iodemo; import java.io.Serializable; import java.util.Date;
public class Student implements Serializable { //序列化接口 /** *序列化的版本ID */ private static final long serialVersionUID = 1L; private String stu_id; private String name; private Date birthday; public Student() { super(); } public Student(String stu_id, String name, Date birthday) { super(); this.stu_id = stu_id; this.name = name; this.birthday = birthday; } public String getStu_id() { return stu_id; } public void setStu_id(String stu_id) { this.stu_id = stu_id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } }



序列化

package com.jredu.iodemo; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.text.ParseException; import java.text.SimpleDateFormat;
//序列化:把对象的状态存储到特定存储介质中的过程 //反序列化:从文件中读取对象的状态 //类通过实现 java.io.Serializable 接口以启用其序列化功能. //未实现此接口的类将无法使其任何状态序列化或反序列化. public class xuliehuaDemo { private static void xie(Student stu) { try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("G:/IO数据/stu.txt")); oos.writeObject(stu); oos.flush(); oos.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } private static void du() { try { ObjectInputStream ois = new ObjectInputStream(new FileInputStream("G:/IO数据/stu.txt")); Object obj = ois.readObject(); ois.close(); if (obj!=null) { Student s1 = (Student)obj; System.out.println(s1.getName()+" "+s1.getBirthday()); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public static void main(String[] args) { Student stu = new Student(); stu.setStu_id("170701"); stu.setName("lizuowei"); SimpleDateFormat sf = new SimpleDateFormat("yyyy/MM/dd"); try { stu.setBirthday(sf.parse("1996/04/06")); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } // xie(stu); // du(); } }

结果图:

     

序列化传递的是一个对象,不是二进制码,不能直接显示

message类
package com.jredu.iodemo;
public class MyException extends RuntimeException { /** * */ private static final long serialVersionUID = 1L; private String message; public MyException() { super(); } public MyException(String message) { super(); this.message = message; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
异常处理
package com.jredu.iodemo;
//throw throws public class TestException2 { public void throwMyException() throws MyException { System.out.println("我要抛异常了..."); throw new MyException("我自定义的异常"); } public static void main(String[] args) { TestException2 t2 = new TestException2(); try { t2.throwMyException(); } catch (MyException e) { System.out.println("我来处理"); System.out.println(e.getMessage()); //e.printStackTrace(); MyLog.log(e.getMessage()+" "+t2.getClass().toString()); } } }
return关键字和finally
package com.jredu.iodemo;
//有return时
public class TestException3 {
    public  int test(){
        int n=10;    //10    
        try{
            n++;    //11
            int m=9/0;//异常
            n++;
            return n;
        }catch(Exception e){
            n=n+1;    //12
            return n;
            
        }finally{
            n=30;
            System.out.println("finally");    
        }
        
        
    }
    public static void main(String[] args) {
        TestException3 result = new TestException3();
        System.out.println(result.test());
    }

}


 

结果图:

 

原文地址:https://www.cnblogs.com/lizuowei/p/7475136.html