/** |
* 对象输出到XML文件 |
* @param obj 待输出的对象 |
* @param outFileName 目标XML文件的文件名 |
* @return 返回输出XML文件的路径 |
* @throws FileNotFoundException |
*/ |
public static String object2XML(Object obj, String outFileName) |
throws FileNotFoundException { |
// 构造输出XML文件的字节输出流 |
File outFile = new File(outFileName); |
BufferedOutputStream bos = new BufferedOutputStream( |
new FileOutputStream(outFile)); |
// 构造一个XML编码器 |
XMLEncoder xmlEncoder = new XMLEncoder(bos); |
// 使用XML编码器写对象 |
xmlEncoder.writeObject(obj); |
// 关闭编码器 |
xmlEncoder.close(); |
|
return outFile.getAbsolutePath(); |
} |