ClassA
package com.seri;
import java.io.Serializable;
/**
* 實現了Serializable接口的對象,可將它們轉換成一系列字節,並可在以後完全恢復回原來的樣子。
* 這一過程亦可通過網絡進行。這意味着序列化機制能自動補償操作系統間的差異。
* 換句話說,可以先在Windows機器上創建一個對象,對其序列化,然後通過網絡發給一臺Unix機器,然後在那裏準確無誤地重新“裝配”。
* 不必關心數據在不同機器上如何表示,也不必關心字節的順序或者其他任何細節。
*
* 實際上,序列化的思想是 “凍結” 對象狀態,傳輸對象狀態(寫到磁盤、通過網絡傳輸等等),然後 “解凍” 狀態,重新獲得可用的 Java 對象。
* 所有這些事情的發生有點像是魔術,這要歸功於 ObjectInputStream/ObjectOutputStream 類、完全保真的元數據以及程序員願意用
* Serializable 標識接口標記他們的類,從而 “參與” 這個過程。
*/
public class ClassA implements Serializable {
/**
* 爲保證 serialVersionUID 值跨不同 java 編譯器實現的一致性,序列化類必須聲明一個明確的 serialVersionUID 值
*/
private static final long serialVersionUID = 6013572251564847381L;
private String name = "My name is a";
private ClassB b = null;
ClassA() {
b = new ClassB();
}
public String show() {
System.out.println("a.toString <a.name=\"" + this.name
+ "\" a.b.name=\"" + this.b.getName() + "\">");
// return "a.toString <a.name=\""+this.name+"\"
// a.b.name=\""+this.b.getName()+"\">";
// result:write obj:a.toString <a.name="My name is a" a.b.name="My name
// is b">
return "a.toString <a.name=" + this.name + "a.b.name="
+ this.b.getName() + ">";
// result:write obj:a.toString <a.name=My name is aa.b.name=My name is
// b>
// \ 作用是轉義 "(雙引號)
// \' '(單引號)
// \\ 反斜線
}
public int add(int a, int b) {
return a + b;
}
public int divide(int a, int b) throws Exception {
if (0 == b) {
throw new Exception("除數不能爲零");
}
return a / b;
}
public int exception(int a, int b) throws Exception {
if (a == 2) {
throw new Exception("進入我設置的異常啦!");
}
return a + b;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ClassB getB() {
return b;
}
public void setB(ClassB b) {
this.b = b;
}
}
import java.io.Serializable;
/**
* 實現了Serializable接口的對象,可將它們轉換成一系列字節,並可在以後完全恢復回原來的樣子。
* 這一過程亦可通過網絡進行。這意味着序列化機制能自動補償操作系統間的差異。
* 換句話說,可以先在Windows機器上創建一個對象,對其序列化,然後通過網絡發給一臺Unix機器,然後在那裏準確無誤地重新“裝配”。
* 不必關心數據在不同機器上如何表示,也不必關心字節的順序或者其他任何細節。
*
* 實際上,序列化的思想是 “凍結” 對象狀態,傳輸對象狀態(寫到磁盤、通過網絡傳輸等等),然後 “解凍” 狀態,重新獲得可用的 Java 對象。
* 所有這些事情的發生有點像是魔術,這要歸功於 ObjectInputStream/ObjectOutputStream 類、完全保真的元數據以及程序員願意用
* Serializable 標識接口標記他們的類,從而 “參與” 這個過程。
*/
public class ClassA implements Serializable {
/**
* 爲保證 serialVersionUID 值跨不同 java 編譯器實現的一致性,序列化類必須聲明一個明確的 serialVersionUID 值
*/
private static final long serialVersionUID = 6013572251564847381L;
private String name = "My name is a";
private ClassB b = null;
ClassA() {
b = new ClassB();
}
public String show() {
System.out.println("a.toString <a.name=\"" + this.name
+ "\" a.b.name=\"" + this.b.getName() + "\">");
// return "a.toString <a.name=\""+this.name+"\"
// a.b.name=\""+this.b.getName()+"\">";
// result:write obj:a.toString <a.name="My name is a" a.b.name="My name
// is b">
return "a.toString <a.name=" + this.name + "a.b.name="
+ this.b.getName() + ">";
// result:write obj:a.toString <a.name=My name is aa.b.name=My name is
// b>
// \ 作用是轉義 "(雙引號)
// \' '(單引號)
// \\ 反斜線
}
public int add(int a, int b) {
return a + b;
}
public int divide(int a, int b) throws Exception {
if (0 == b) {
throw new Exception("除數不能爲零");
}
return a / b;
}
public int exception(int a, int b) throws Exception {
if (a == 2) {
throw new Exception("進入我設置的異常啦!");
}
return a + b;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public ClassB getB() {
return b;
}
public void setB(ClassB b) {
this.b = b;
}
}
package com.seri;
import java.io.Serializable;
public class ClassB implements Serializable{
private static final long serialVersionUID = -4324044767844361076L;
private String name="My name is b";
ClassB(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
import java.io.Serializable;
public class ClassB implements Serializable{
private static final long serialVersionUID = -4324044767844361076L;
private String name="My name is b";
ClassB(){}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
WriteSeri
package com.seri;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class WriteSeri {
public static void main(String args[]){
ObjectOutputStream outObj=null;
try {
FileOutputStream outStr=new FileOutputStream("obj.txt");
outObj=new ObjectOutputStream(outStr);
ClassA a=new ClassA();
outObj.writeObject(a);
System.out.println("write obj:"+a.show());
outObj.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (outObj != null) {
outObj.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class WriteSeri {
public static void main(String args[]){
ObjectOutputStream outObj=null;
try {
FileOutputStream outStr=new FileOutputStream("obj.txt");
outObj=new ObjectOutputStream(outStr);
ClassA a=new ClassA();
outObj.writeObject(a);
System.out.println("write obj:"+a.show());
outObj.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if (outObj != null) {
outObj.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
ReadSeri
package com.seri;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ReadSeri {
public static void main(String args[]) {
ObjectInputStream inObj = null;
try {
FileInputStream inStr = new FileInputStream("obj.txt");
inObj = new ObjectInputStream(inStr);
ClassA a = (ClassA) inObj.readObject();
System.out.println("read object:" + a.show());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (inObj != null) {
try {
inObj.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class ReadSeri {
public static void main(String args[]) {
ObjectInputStream inObj = null;
try {
FileInputStream inStr = new FileInputStream("obj.txt");
inObj = new ObjectInputStream(inStr);
ClassA a = (ClassA) inObj.readObject();
System.out.println("read object:" + a.show());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if (inObj != null) {
try {
inObj.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
ClassATest
package com.seri;
import junit.framework.Assert;
import junit.framework.TestCase;
public class ClassATest extends TestCase {
ClassA a = new ClassA();
public void testShow() {
// 覆蓋原來的name
a.setName("hello TestA");
a.getB().setName("hello TestB");
a.show();
}
public void testAdd() {
// boolean b=false;
// Assert.assertEquals(true, b);
int result = 0;
result = a.add(2, 5);
Assert.assertEquals(7, result);
}
public void testDivide() {
int result = 0;
try {
result = a.divide(10, 5);
} catch (Exception e) {
e.printStackTrace(); // 如果進入到catch中說明執行divide失敗
System.out.println("執行上面那句話了");
Assert.fail();
}
Assert.assertEquals(2, result);
}
public void testException() {
int result = 0;
try {
result = a.exception(3, 4);
} catch (Exception e) {
e.printStackTrace();
System.out.println("執行上面那句話了");
Assert.fail();
}
Assert.assertEquals(7, result);
}
}
import junit.framework.Assert;
import junit.framework.TestCase;
public class ClassATest extends TestCase {
ClassA a = new ClassA();
public void testShow() {
// 覆蓋原來的name
a.setName("hello TestA");
a.getB().setName("hello TestB");
a.show();
}
public void testAdd() {
// boolean b=false;
// Assert.assertEquals(true, b);
int result = 0;
result = a.add(2, 5);
Assert.assertEquals(7, result);
}
public void testDivide() {
int result = 0;
try {
result = a.divide(10, 5);
} catch (Exception e) {
e.printStackTrace(); // 如果進入到catch中說明執行divide失敗
System.out.println("執行上面那句話了");
Assert.fail();
}
Assert.assertEquals(2, result);
}
public void testException() {
int result = 0;
try {
result = a.exception(3, 4);
} catch (Exception e) {
e.printStackTrace();
System.out.println("執行上面那句話了");
Assert.fail();
}
Assert.assertEquals(7, result);
}
}