The NoteBook of EricKong

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class TestXML {

 
    
public static void objectXmlEncoder(Object obj, String fileName)
            
throws FileNotFoundException, IOException, Exception {
        
// 创建输出文件
        File fo = new File(fileName);
        
// 文件不存在,就创建该文件
        if (!fo.exists()) {
            
// 先创建文件的目录
            String path = fileName.substring(0, fileName.lastIndexOf('.'));
            File pFile 
= new File(path);
            pFile.mkdirs();
        }

        
// 创建文件输出流
        FileOutputStream fos = new FileOutputStream(fo);
        
// 创建XML文件对象输出类实例
        XMLEncoder encoder = new XMLEncoder(fos);
        
// 对象序列化输出到XML文件
        encoder.writeObject(obj);
        encoder.flush();
        
// 关闭序列化工具
        encoder.close();
        
// 关闭输出流
        fos.close();
    }


 
    
public static List objectXmlDecoder(String objSource) throws FileNotFoundException, IOException, Exception {
        List objList 
= new ArrayList();
        File fin 
= new File(objSource);
        FileInputStream fis 
= new FileInputStream(fin);
        XMLDecoder decoder 
= new XMLDecoder(fis);
        Object obj 
= null;
        
try {
            
while ((obj = decoder.readObject()) != null{
                objList.add(obj);
            }

        }
 catch (Exception e) {
             
        }

        fis.close();
        decoder.close();
        
return objList;
    }


}
posted on 2012-02-23 16:28 Eric_jiang 阅读(388) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: