JAVA中關於對像的讀寫

來源:互聯網
上載者:User

標籤:des   cti   txt   分享   轉換   集合   blog   exce   tput   

 1 /** 2  * 針對對象的檔案讀寫 3  */ 4  5 //匯入包 6 import java.io.File; 7 import java.io.FileInputStream; 8 import java.io.FileNotFoundException; 9 import java.io.FileOutputStream;10 import java.io.IOException;11 import java.io.ObjectInputStream;12 import java.io.ObjectOutputStream;13 import java.util.HashMap;14 15 public class qwe {16 17     public static void main(String[] args) {18         try {19             File f = new File("D:\\Desktop\\NewFile\\hasKey.txt");//關聯檔案20             FileOutputStream wr = new FileOutputStream("D:\\Desktop\\NewFile\\hasKey.txt");//檔案寫21             ObjectOutputStream objw = new ObjectOutputStream(wr);//建立對象輸出資料流22             FileInputStream re = new FileInputStream("D:\\Desktop\\NewFile\\hasKey.txt");//讀檔案23             ObjectInputStream objr = new ObjectInputStream(re); //建立對象輸入資料流24             HashMap<String,String> has = new HashMap<String,String>();//建立集合25             has.put("001", "hello")    ;                26             27             objw.writeObject(has);//從對象輸出資料流中寫入                                28             HashMap<String, String> has1 = (HashMap<String, String>) objr.readObject();//從對象輸入資料流中讀(強制轉換類型)29             30             System.out.println(has1);                        31                                                              32         } catch (FileNotFoundException e) {//無檔案異常捕獲33             // TODO Auto-generated catch block34             e.printStackTrace();35         }36         catch (IOException e) {//捕獲IO異常37             // TODO Auto-generated catch block38             e.printStackTrace();39         }catch (ClassNotFoundException e) {//捕獲不存在的類型的異常40                 41                 e.printStackTrace();42             }43         44         45     }46 47 }

運行結果為

 

存入文本的資料是

JAVA中關於對像的讀寫

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.