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

来源:互联网 发布:pc机安装mac 编辑:程序博客网 时间:2024/05/29 13:54
/**   
* @Title: FileRW.java
* @Package com.file
* @Description: 文件、文件夹的创建、写入练习。读写是使用对象流实现。
* @author 慢跑学Android
* @date 2011-11-19 下午03:53:01
* @version V1.0   
*/
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;
    }

}


/**   
* @Title: TestMessage.java
* @Package com.file
* @Description: FileRW的消息流
* @author 慢跑学Android
* @date 2011-11-19 下午04:35:11
* @version V1.0   
*/
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;
    }
}

0 0