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;
}
}
相關文章
- ES7 Object.keys,Object.values,Object.entriesObject
- ObjectObject
- 介面返回[object,Object]解決方法Object
- MXRuntimeUtils,替代 [NSObject performSelector object object ]的工具ObjectperformSelector
- object類Object
- Object.seal()與Object.freeze()區別Object
- Object.is()與'==='Object
- Object Runtime -- WeakObject
- Object.getOwnPropertyNames()Object
- Object.getPrototypeOf()Object
- Object.freeze()Object
- Object.getOwnPropertyDescriptor()Object
- JavaScript Object 物件JavaScriptObject物件
- Object.defineProperties()Object
- Object.is()方法Object
- object-detectionObject
- Object.create()Object
- Object.setPrototypeOf()Object
- Object.defineProperty()Object
- Object.seal()Object
- Object.preventExtensions()Object
- Object.keys()Object
- Object.isExtensible()Object
- Object.isSealed()Object
- Object.isFrozen()Object
- Dictionary<string, object>Object
- object.assignObject
- Iterable object of JavaScriptObjectJavaScript
- [譯] Object.assign 和 Object Spread 之爭, 用誰?Object
- TypeScript 中令人迷惑的物件型別:Object、{} 和 objectTypeScript物件型別Object
- fastjson判斷JSON字串是Object還是List<Object>ASTJSON字串Object
- javascript-Object.defineProperty()JavaScriptObject
- Object方法與ReflectObject
- Object.assign()方法Object
- JavaScript Object.values()JavaScriptObject
- js列印object物件JSObject物件
- JavaScript Object.entries()JavaScriptObject
- TypeError: object() takes no parametersErrorObject