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

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

测试主程序:

package com.file;  

import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileNotFoundException;  
import java.io.FileOutputStream;  
import java.io.IOException;  
import java.io.ObjectInputStream;  
import java.io.ObjectOutputStream;  
import java.util.LinkedList;  


public class FileRW {  
    private String dirPath;  
    private String filename;  

    public static void main(String[] args) {  
        String path = "C:\\晓声";  
        String fileName = "test.txt";  
        FileRW fileRW = new FileRW(path, fileName);  
        LinkedList<TestMessage> msgOut = new LinkedList<TestMessage>();  
        LinkedList<TestMessage> msgIn = null;  

        msgOut.add(new TestMessage("柯南", "偶像"));  
        msgOut.add(new TestMessage("卡卡西", "好样的"));  
        msgOut.add(new TestMessage("Android", "Android"));  
        msgOut.add(new TestMessage("哈哈", "测试下喔"));  
        fileRW.writeObject(path, fileName, msgOut);  

        msgIn = fileRW.readObject(path,fileName);  

        for(TestMessage temp:msgIn) {  
            System.out.println(temp.getName() + temp.getData());  
        }  

    }  

    public FileRW(String dirPath, String filename) {  
        this.dirPath = dirPath;  
        this.filename = filename;  
        if (creatDir()) {             
            creatFile();  
        }  
    }  


    private boolean creatDir() {  
        if (null != dirPath) {  
            File path = new File(dirPath);  
            if (path.exists()) {  
                return true;  
            }  
            if (true == path.mkdirs() ) {  
                return true;  
            }  
        }  
        return false;  
    }  

    private void creatFile() {  
        if (null != filename) {  
            File file = new File(dirPath, filename);  
            if (false == file.exists()) {  
                try {  
                    file.createNewFile();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
    }  


    /** 
    * @Title: writeObject 
    * @Description: Write a object to a file. 
    * @param path the directory of the target file 
    * @param filename the name of the target file 
    * @param msg the type of the object 
    * @return void 
    * @throws 
    */  
    private void writeObject(String path, String filename, LinkedList<TestMessage> msg) {  
        File file = new File(path, filename);  
        if (false == file.isFile()) {  
            return ;  
        }  

        try {  
            // The value "false" for FileOutputStream means that overwrite this file,  
            // if it is "true",append the new data to this file.  
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file,false));  
            oos.writeObject(msg);  
            oos.flush();  
            oos.close();  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  

    /** 
    * @Title: readObject 
    * @Description: Read a object from a file. 
    * @param path the directory of the target file 
    * @param filename the name of the target file 
    * @return LinkedList<TestMessage> 
    * @throws 
    */  
    @SuppressWarnings("unchecked")  
    private LinkedList<TestMessage> readObject(String path, String filename) {  
        File file = new File(path, filename);  
        ObjectInputStream ois = null;  
        LinkedList<TestMessage> msgAll = null;  

        try {  
            ois = new ObjectInputStream(new FileInputStream(file));  
            try {  
                msgAll = (LinkedList<TestMessage>)ois.readObject();  

            } catch (ClassNotFoundException e) {  
                e.printStackTrace();  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            try {  
                ois.close();  
            } catch (IOException e) {  
                e.printStackTrace();  
            }  
        }  

        return msgAll;  
    }  
}  

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


package com.file;  


public class TestMessage implements java.io.Serializable {  
    private String name;  
    private String data;  

    public String getName() {  
        return name;  
    }  
    public void setName(String name) {  
        this.name = name;  
    }  
    public String getData() {  
        return data;  
    }  
    public void setData(String data) {  
        this.data = data;  
    }  
    public TestMessage(String name, String msg) {  
        this.name = name;  
        data = msg;  
    }  
}  

转载地址:

http://blog.csdn.net/u010355144/article/details/47951429

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值