码迷,mamicode.com
首页 > 其他好文 > 详细

使用对象流将数据以对象形式进行读写

时间:2014-08-14 23:47:56      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   java   使用   os   io   数据   

 1 import java.io.*;
 2 
 3 public class Test {
 4 
 5     public static void main(String[] args) {
 6         
 7         File f = new File("a.txt");
 8         try {
 9             if(!f.exists())
10                 f.createNewFile();
11         } catch (IOException e1) {
12             e1.printStackTrace();
13         }
14         
15         try {
16             ObjectOutputStream oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("a.txt")));
17             Employe em = new Employe("xiaoming","123");
18             oos.writeObject(em);
19             oos.close();
20             
21             ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream("a.txt")));
22             em = (Employe)ois.readObject();
23             System.out.println(em.name);
24             
25         } catch (FileNotFoundException e) {
26             e.printStackTrace();
27         } catch (IOException e) {
28             e.printStackTrace();
29         } catch (ClassNotFoundException e) {
30             e.printStackTrace();
31         }        
32     }
33 }
34 
35 class Employe implements Serializable {             //对象输出流的对象所对应的类应该实现Serializable接口
36     String name;
37     String id;
38     
39     Employe(String name,String id){
40         this.name = name;
41         this.id = id;    
42     }    
43 }

 

FileOutputStream();还不太明白

 

使用对象流将数据以对象形式进行读写,布布扣,bubuko.com

使用对象流将数据以对象形式进行读写

标签:style   blog   color   java   使用   os   io   数据   

原文地址:http://www.cnblogs.com/Arvin-9/p/3913646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!