關(guān)于政府網(wǎng)站建設(shè)的實施意見個人網(wǎng)頁免費域名注冊入口
文章目錄
- 一、對象流的使用
- 1.概念
- 2.序列化機制
- 3.代碼案例:序列化過程:將內(nèi)存中的java對象保存到磁盤中或通過通絡(luò)傳輸出去
- 4.反序列化,將磁盤文件中的對象還原為內(nèi)存中的一個java對象
- 二、RandomAccessFile的使用
- 1.說明
- 2.代碼案例
提示:以下是本篇文章正文內(nèi)容,下面案例可供參考
一、對象流的使用
1.概念
1.ObjectInputStream和ObjectOutputStream
2.作用:用于存儲和讀取基本數(shù)據(jù)類型數(shù)據(jù)或對象的處理流
3.要想一個java對象是可序列化的,需要滿足相應(yīng)的要求。
1.需要實現(xiàn)接口:Serializable
2.當前類提供一個全局常量:serialVersionUID
3.處理當前Person類需要實現(xiàn)Serializable接口之外,還必須保證其內(nèi)部所有屬性也必須是可序列化的(默認情況下,基本數(shù)據(jù)類型可序列化)補充:.ObjectInputStream和ObjectOutputStream不能序列化static和transient修飾的成員變量。
2.序列化機制
對象序列化機制允許把內(nèi)存中的Java對象轉(zhuǎn)換成平臺無關(guān)的二進制流,從而允許把這種二進制流持久地保存在磁盤上,或通過網(wǎng)絡(luò)將這種二進制流傳輸?shù)搅硪粋€網(wǎng)絡(luò)節(jié)點。當其它程序獲取了這種二進制流,就可以恢復(fù)成原來的Java對象。
3.代碼案例:序列化過程:將內(nèi)存中的java對象保存到磁盤中或通過通絡(luò)傳輸出去
//序列化過程:將內(nèi)存中的java對象保存到磁盤中或通過通絡(luò)傳輸出去//使用ObjectOutputStream實現(xiàn)@Testpublic void testObjectOutputStream(){ObjectOutputStream oos = null;try{oos = new ObjectOutputStream(new FileOutputStream("object.dat"));oos.writeObject(new String("我愛北京天安門"));oos.flush(); //刷新操作oos.writeObject(new Person("姚瑪麗",21));oos.flush();}catch (IOException e){e.printStackTrace();}finally {try{if(oos != null)oos.close();}catch(IOException e){e.printStackTrace();}}}
4.反序列化,將磁盤文件中的對象還原為內(nèi)存中的一個java對象
@Testpublic void testObjectInputStream(){ObjectInputStream ois = null;try{ois = new ObjectInputStream(new FileInputStream("object.dat"));Object obj = ois.readObject();String str = (String)obj;Person p = (Person)ois.readObject();System.out.println(str);System.out.println(p);}catch(IOException e){e.printStackTrace();} catch (ClassNotFoundException e) {e.printStackTrace();} finally {try{if(ois != null)ois.close();}catch(IOException e){e.printStackTrace();}}}public class Person implements Serializable {public static final long sericlVersionUID = 45866454556L;private String name;private int age;public void setName(String name){this.name = name;}public String getName(){return this.name;}public String toString(){return "Person{" +"name='"+name+'\''+",age="+age;}public Person(){}public Person(String name,int age){this.name = name;this.age = age;}}
二、RandomAccessFile的使用
1.說明
1.RandomAccessFile直接繼承于java.lang.Object類,實現(xiàn)了DataInput和DataOutput接口
2.RandomAccessFile既可以作為一個輸入流,又可以作為一個輸出流
3.如果RandomAccessFile作為輸出流時,寫出到的文件如果不存在,則在執(zhí)行過程中自動創(chuàng)建,如果寫出到的文件存在,則會對原有文件內(nèi)容進行覆盞。(默認情況下,從頭覆蓋)
4.可以通過相關(guān)的操作,實現(xiàn)RandomAccessFile“插入”數(shù)據(jù)的效果
2.代碼案例
public void test1() throws FileNotFoundException {RandomAccessFile raf1 = null;RandomAccessFile raf2 = null;try{raf1 = new RandomAccessFile(new File("2.jpg"),"r");raf2 = new RandomAccessFile(new File("3.jpg"),"rw");byte[] buffer = new byte[1024];int len;while((len = raf1.read(buffer))!=-1){raf2.write(buffer,0,len);}}catch (IOException e){e.printStackTrace();}finally {if(raf1!=null){try{raf1.close();}catch(IOException e){e.printStackTrace();}}if(raf2!=null){try{raf2.close();}catch(IOException e){e.printStackTrace();}}}}
public void test2() throws IOException{RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");raf1.seek(3); //將指針調(diào)到角標為3的位置raf1.write("xyz".getBytes());raf1.close();}
//使用RandomAccessFile實現(xiàn)數(shù)據(jù)的插入效果@Testpublic void test3() throws IOException{RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw");raf1.seek(3); //將指針調(diào)到角標為3的位置//保存指針3后面的所有數(shù)據(jù)到StringBuilder中StringBuilder builder = new StringBuilder((int)new File("hello.txt").length());byte[] buffer = new byte[20];int len;while((len = raf1.read(buffer)) != -1){builder.append(new String(buffer,0,len));}//調(diào)回指針,寫入“xyz”raf1.seek(3);raf1.write("xyz".getBytes());//將StringBuilder中的數(shù)據(jù)寫入到文件中raf1.write(builder.toString().getBytes());raf1.close();}