Java 如何对文件进行多个Object对象流的读写操作

思路:把已经序列化的对象存入容器(如LinkedList<?>)中,然后用ObjectInputStream和ObjectOutputStream对这个实例化的LinkedList<?>对象进行读写。


测试主程序:

[java]  view plain copy
  1.   
[java]  view plain copy
  1. /**    
  2. * @Title: FileRW.java 
  3. * @Package com.file 
  4. * @Description: 文件、文件夹的创建、写入练习。读写是使用对象流实现。 
  5. * @author 慢跑学Android 
  6. * @date 2011-11-19 下午03:53:01 
  7. * @version V1.0    
  8. */  
  9. package com.file;  
  10.   
  11. import java.io.File;  
  12. import java.io.FileInputStream;  
  13. import java.io.FileNotFoundException;  
  14. import java.io.FileOutputStream;  
  15. import java.io.IOException;  
  16. import java.io.ObjectInputStream;  
  17. import java.io.ObjectOutputStream;  
  18. import java.util.LinkedList;  
  19.   
  20.   
  21. public class FileRW {  
  22.     private String dirPath;  
  23.     private String filename;  
  24.       
  25.     public static void main(String[] args) {  
  26.         String path = "C:\\晓声";  
  27.         String fileName = "test.txt";  
  28.         FileRW fileRW = new FileRW(path, fileName);  
  29.         LinkedList<TestMessage> msgOut = new LinkedList<TestMessage>();  
  30.         LinkedList<TestMessage> msgIn = null;  
  31.           
  32.         msgOut.add(new TestMessage("柯南""偶像"));  
  33.         msgOut.add(new TestMessage("卡卡西""好样的"));  
  34.         msgOut.add(new TestMessage("Android""Android"));  
  35.         msgOut.add(new TestMessage("哈哈""测试下喔"));  
  36.         fileRW.writeObject(path, fileName, msgOut);  
  37.           
  38.         msgIn = fileRW.readObject(path,fileName);  
  39.           
  40.         for(TestMessage temp:msgIn) {  
  41.             System.out.println(temp.getName() + temp.getData());  
  42.         }  
  43.           
  44.     }  
  45.       
  46.     public FileRW(String dirPath, String filename) {  
  47.         this.dirPath = dirPath;  
  48.         this.filename = filename;  
  49.         if (creatDir()) {             
  50.             creatFile();  
  51.         }  
  52.     }  
  53.   
  54.       
  55.     private boolean creatDir() {  
  56.         if (null != dirPath) {  
  57.             File path = new File(dirPath);  
  58.             if (path.exists()) {  
  59.                 return true;  
  60.             }  
  61.             if (true == path.mkdirs() ) {  
  62.                 return true;  
  63.             }  
  64.         }  
  65.         return false;  
  66.     }  
  67.   
  68.     private void creatFile() {  
  69.         if (null != filename) {  
  70.             File file = new File(dirPath, filename);  
  71.             if (false == file.exists()) {  
  72.                 try {  
  73.                     file.createNewFile();  
  74.                 } catch (IOException e) {  
  75.                     e.printStackTrace();  
  76.                 }  
  77.             }  
  78.         }  
  79.     }  
  80.       
  81.   
  82.     /** 
  83.     * @Title: writeObject 
  84.     * @Description: Write a object to a file. 
  85.     * @param path the directory of the target file 
  86.     * @param filename the name of the target file 
  87.     * @param msg the type of the object 
  88.     * @return void 
  89.     * @throws 
  90.     */  
  91.     private void writeObject(String path, String filename, LinkedList<TestMessage> msg) {  
  92.         File file = new File(path, filename);  
  93.         if (false == file.isFile()) {  
  94.             return ;  
  95.         }  
  96.           
  97.         try {  
  98.             // The value "false" for FileOutputStream means that overwrite this file,  
  99.             // if it is "true",append the new data to this file.  
  100.             ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,false));  
  101.             oos.writeObject(msg);  
  102.             oos.flush();  
  103.             oos.close();  
  104.         } catch (FileNotFoundException e) {  
  105.             e.printStackTrace();  
  106.         } catch (IOException e) {  
  107.             e.printStackTrace();  
  108.         }  
  109.     }  
  110.       
  111.     /** 
  112.     * @Title: readObject 
  113.     * @Description: Read a object from a file. 
  114.     * @param path the directory of the target file 
  115.     * @param filename the name of the target file 
  116.     * @return LinkedList<TestMessage> 
  117.     * @throws 
  118.     */  
  119.     @SuppressWarnings("unchecked")  
  120.     private LinkedList<TestMessage> readObject(String path, String filename) {  
  121.         File file = new File(path, filename);  
  122.         ObjectInputStream ois = null;  
  123.         LinkedList<TestMessage> msgAll = null;  
  124.           
  125.         try {  
  126.             ois = new ObjectInputStream(new FileInputStream(file));  
  127.             try {  
  128.                 msgAll = (LinkedList<TestMessage>)ois.readObject();  
  129.                   
  130.             } catch (ClassNotFoundException e) {  
  131.                 e.printStackTrace();  
  132.             }  
  133.         } catch (FileNotFoundException e) {  
  134.             e.printStackTrace();  
  135.         } catch (IOException e) {  
  136.             e.printStackTrace();  
  137.         } finally {  
  138.             try {  
  139.                 ois.close();  
  140.             } catch (IOException e) {  
  141.                 e.printStackTrace();  
  142.             }  
  143.         }  
  144.           
  145.         return msgAll;  
  146.     }  
  147. }  



测试程序中的消息包定义:

[java]  view plain copy
  1. /**    
  2. * @Title: TestMessage.java 
  3. * @Package com.file 
  4. * @Description: FileRW的消息流 
  5. * @author 慢跑学Android 
  6. * @date 2011-11-19 下午04:35:11 
  7. * @version V1.0    
  8. */  
  9. package com.file;  
  10.   
  11.   
  12. public class TestMessage implements java.io.Serializable {  
  13.     private String name;  
  14.     private String data;  
  15.       
  16.     public String getName() {  
  17.         return name;  
  18.     }  
  19.     public void setName(String name) {  
  20.         this.name = name;  
  21.     }  
  22.     public String getData() {  
  23.         return data;  
  24.     }  
  25.     public void setData(String data) {  
  26.         this.data = data;  
  27.     }  
  28.     public TestMessage(String name, String msg) {  
  29.         this.name = name;  
  30.         data = msg;  
  31.     }  
  32. }  

程序运行结果:


参考资料:ObjectInputStream类和ObjectOutputStream类的使用


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值