思路:把已经序列化的对象存入容器(如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

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐