java beans XMLEncoder和XMLDecoder的示例
package test;
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
public class Test
{
?public static void main(String[] args)
?{
//??File file = new File("/");
//??String path = file.getPath();
//??String path2 = file.getAbsolutePath();
//??System.out.println("file1 " + path);
//??System.out.println("file2 " + path2);
//??System.out.println(Thread.currentThread().getContextClassLoader()
//????.getResource(""));
//
//??System.out.println(Test.class.getClassLoader().getResource(""));
//
//??System.out.println(ClassLoader.getSystemResource("") + " 1");
//??System.out.println(Test.class.getResource(""));
//??System.out.println(Test.class.getResource("/") + " 2"); // Class文件所在路径
//??System.out.println(new File("/").getAbsolutePath());
//??System.out.println(System.getProperty("file.separator"));
??try
??{
//???FileReader fr = new FileReader(new File(
//?????System.getProperty("user.dir") + "/a.xml"));
//???BufferedReader b = new BufferedReader(fr);
//???try
//???{
//????System.out.println(b.readLine());
//???} catch (IOException e)
//???{
//????// TODO Auto-generated catch block
//????e.printStackTrace();
//???}
???
???FileInputStream f = new FileInputStream(new File(
???System.getProperty("user.dir") + "/a.xml"));
???XMLDecoder xmlDec = new XMLDecoder(new BufferedInputStream(f));
???TestPojo o = (TestPojo)xmlDec.readObject();
???System.out.println(o.getName());
???
??} catch (FileNotFoundException e)
??{
???// TODO Auto-generated catch block
???e.printStackTrace();
??}
//?? try
//??{
//???XMLEncoder xmle=new XMLEncoder(new BufferedOutputStream(new FileOutputStream(System.getProperty("user.dir") + "/a.xml")));
//???TestPojo tp=new TestPojo();
//???tp.setName("asdf");
//???xmle.writeObject(tp);
//???xmle.close();
//??} catch (FileNotFoundException e)
//??{
//???// TODO Auto-generated catch block
//???e.printStackTrace();
//??}
?}
}