Object流
Object流把一個資料儲存到檔案當中,檔案中的資料是亂碼,從檔案中讀取資料,讀出來的資料就是原來輸入的資料
import java.io.*;
public class Test{
public static void main(String[] args) throws Exception{
ObjectOutputStream osc=null;
ObjectInputStream osr=null;
Student ss=new Student("悟空",10,95.5f);
Student ss2=null;
try{
FileOutputStream wsc=new FileOutputStream("E:/aaa.txt");
osc=new ObjectOutputStream(wsc);
osc.writeObject(ss);
osr=new ObjectInputStream(new FileInputStream("E:/aaa.txt"));
ss2=(Student)osr.readObject();
//readObject()方法就是用於讀取資料,並且要進行型別轉換
System.out.println("姓名:"+ss2.xingming);
System.out.println("學號:"+ss2.xuehao);
System.out.println("成績:"+ss2.chengji);
}
catch(Exception e){
System.out.println("出現錯誤");
}
finally{//作用是善後
try{
osc.close();
osr.close();
System.exit(-1);
}
catch(Exception e){
System.exit(-1);
}
//finally下面的try catch無論是都要寫上起到退出作用的話
//如果try catch的後面有finally,那麼try catch中不要寫退出程式的程式碼
}
}
}
class Student implements Serializable{//介面是空的,起的是標誌作用
String xingming=null;
int xuehao=0;
transient float chengji=0;
//transient的作用是忽略,外界傳不進來資料
public Student(String xingming,int xuehao,float chengji){
this.xingming=xingming;
this.xuehao=xuehao;
this.chengji=chengji;
}
}
相關文章
- Uncaught TypeError: Object [object Object] has no method 'xxx'ErrorObject
- ObjectObject
- 介面返回[object,Object]解決方法Object
- Effective Object C 2.0 『熟悉Object C』Object
- ES7 Object.keys,Object.values,Object.entriesObject
- Object被其他的Object引用的SQLObjectSQL
- OBJECT_ID和DATA_OBJECT_IDObject
- 其它 ObjectObject
- object in javascriptObjectJavaScript
- Authorization ObjectObject
- object類Object
- Differences between DATA_OBJECT_ID and OBJECT_IDObject
- object_id 及 data_object_id (oracle)ObjectOracle
- Object.seal()與Object.freeze()區別Object
- CSS3 object-fit 和 object-positionCSSS3Object
- object_id and data_object_id in dba_objectsObject
- Iterable object of JavaScriptObjectJavaScript
- JavaScript Object 物件JavaScriptObject物件
- Object.defineProperty()Object
- Object.isSealed()Object
- Object.isFrozen()Object
- Object.defineProperties()Object
- Object.is()方法Object
- Object.seal()Object
- Object.preventExtensions()Object
- Object.create()Object
- Object.setPrototypeOf()Object
- Object.getOwnPropertyNames()Object
- Object.getPrototypeOf()Object
- Object.getOwnPropertyDescriptor()Object
- Object.is()與'==='Object
- object-detectionObject
- JAVA Object類JavaObject
- Object assign()Object
- LOB(large object)Object
- javascript Object分析JavaScriptObject
- MXRuntimeUtils,替代 [NSObject performSelector object object ]的工具ObjectperformSelector
- TypeScript 中令人迷惑的物件型別:Object、{} 和 objectTypeScript物件型別Object