本文整理汇总了Java中java.beans.XMLEncoder.writeObject方法的典型用法代码示例。如果您正苦于以下问题:Java XMLEncoder.writeObject方法的具体用法?Java XMLEncoder.writeObject怎么用?Java XMLEncoder.writeObject使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.beans.XMLEncoder
的用法示例。
在下文中一共展示了XMLEncoder.writeObject方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: write
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public @Override void write(java.io.Writer w, final Object inst) throws IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new ExceptionListener() {
public @Override void exceptionThrown(Exception x) {
Logger.getLogger(XMLBeanConvertor.class.getName()).log(Level.INFO, "Problem writing " + inst, x);
}
});
ClassLoader ccl = Thread.currentThread().getContextClassLoader();
try {
// XXX would inst.getClass().getClassLoader() be more appropriate?
ClassLoader ccl2 = Lookup.getDefault().lookup(ClassLoader.class);
if (ccl2 != null) {
Thread.currentThread().setContextClassLoader(ccl2);
}
e.writeObject(inst);
} finally {
Thread.currentThread().setContextClassLoader(ccl);
}
e.close();
String data = new String(out.toByteArray(), "UTF-8");
data = data.replaceFirst("<java", "<!DOCTYPE xmlbeans PUBLIC \"-//NetBeans//DTD XML beans 1.0//EN\" \"http://www.netbeans.org/dtds/xml-beans-1_0.dtd\">\n<java");
w.write(data);
}
示例2: objectXmlEncoder
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* 把java的可序列化的对象(实现Serializable接口)序列化保存到XML文件里面,如果想一次保存多个可序列化对象请用集合进行封装
* 保存时将会用现在的对象原来的XML文件内容
* @param obj 要序列化的可序列化的对象
* @param fileName 带完全的保存路径的文件名
* @throws FileNotFoundException 指定位置的文件不存在
* @throws IOException 输出时发生异常
* @throws Exception 其他运行时异常
*/
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();
}
示例3: saveAnObjectXML
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public static void saveAnObjectXML(Object obj, String path) {
try {
// Serialize object into XML
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(path)));
encoder.writeObject(obj);
encoder.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
示例4: run
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void run() {
// run thread a few time
// object stays the same but NullPointerException appears randomly
// on dual proccessor a lock is generated
for (int i = 0; i < THREAD_LENGTH; i++) {
// create XMLEncoder to ByteArrayOutputStream
// this is to exclude file locking problems
XMLEncoder encoder = new XMLEncoder(new ByteArrayOutputStream());
encoder.setExceptionListener(this);
// write the object
// will see randomly null pointer exceptions
// a bug as object is same through different encode phases
encoder.writeObject(this.object);
//close encoder
encoder.close();
}
System.out.println(Thread.currentThread().getName() + " is finished");
}
示例5: test
import java.beans.XMLEncoder; //导入方法依赖的package包/类
static void test(AbstractTest object) {
ByteArrayOutputStream output = new ByteArrayOutputStream();
XMLEncoder encoder = new XMLEncoder(output);
encoder.setPersistenceDelegate(
object.getClass(),
new DefaultPersistenceDelegate(new String[] {"value"}));
encoder.writeObject(object);
encoder.close();
System.out.print(output);
ByteArrayInputStream input = new ByteArrayInputStream(output.toByteArray());
XMLDecoder decoder = new XMLDecoder(input);
AbstractTest result = (AbstractTest) decoder.readObject();
decoder.close();
if (object.getValue() != result.getValue())
throw new Error("Should be " + object);
}
示例6: doSave
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* This code abstracted because it relies on java.beans.* which is not always available
* e.g. on Android
*/
@Override
public void
doSave(
OutputStream out,
Map usersMap )
{
UserManagerConfig config = new UserManagerConfig();
List users = new ArrayList( usersMap.values() );
config.setUsers(users);
XMLEncoder encoder = new XMLEncoder( new BufferedOutputStream( out ) );
encoder.writeObject(config);
encoder.close();
}
示例7: objectToXml
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public static byte[] objectToXml(Object obj,ClassLoader cl){
if(cl == null){ return objectToXml(obj); }
ByteArrayOutputStream bos = new ByteArrayOutputStream();
XMLEncoder xml = new XMLEncoder(bos);
//HACK
ClassLoader oldLoader = Thread.currentThread().getContextClassLoader();
if (cl != oldLoader && cl != null){
try{
Thread.currentThread().setContextClassLoader(cl);
xml.writeObject(obj);
}catch(Exception e){e.printStackTrace();}
finally {
Thread.currentThread().setContextClassLoader(oldLoader);
}
}else{
xml.writeObject(obj);
}
xml.close();
return bos.toByteArray();
}
示例8: toXml
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* To xml.
*
* @param obj
* the obj
* @return the string
*/
// ////////////////////////////////////////////////////////////////////
public static String toXml(final Object obj) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
// XStream x = createXStream();
// String xml = x.toXML(obj);
// return xml;
final XMLEncoder e = new XMLEncoder(out);
e.setExceptionListener(new XmlEncoderExceptionListener());
// e.setPersistenceDelegate(Object.class, new MyPersistenceDelegate());
e.writeObject(obj);
e.close();
return out.toString();
// return null;
}
示例9: saveWorkspaceToFile
import java.beans.XMLEncoder; //导入方法依赖的package包/类
private void saveWorkspaceToFile(File file) {
try {
FileOutputStream fos = new FileOutputStream(file);
XMLEncoder encoder = new XMLEncoder(fos);
encoder.writeObject(config);
encoder.close();
recentMenu.addRecentWorkspace(file);
} catch (FileNotFoundException e) {
GUIHelper.showMessage(this, "An error occured while saving to "
+ file.getPath(), JOptionPane.ERROR_MESSAGE);
}
GUIHelper.showMessage(this, "Workspace saved to " + file.getPath(),
JOptionPane.INFORMATION_MESSAGE);
actionSaveWorkspaceAs.setEnabled(true);
}
示例10: writeToDisk
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void writeToDisk() throws IOException {
if (_purged)
return;
XMLEncoder out = null;
try {
out = _um.getXMLEncoder(new SafeFileOutputStream(_um
.getUserFile(getName())));
ClassLoader prevCL = Thread.currentThread().getContextClassLoader();
Thread.currentThread().setContextClassLoader(CommonPluginUtils.getClassLoaderForObject(this));
out.writeObject(this);
Thread.currentThread().setContextClassLoader(prevCL);
logger.debug("Wrote userfile for " + this.getName());
} finally {
if (out != null)
out.close();
}
}
示例11: saveTo
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* Saves segmentation rules into specified file.
*/
public static void saveTo(SRX srx, File outFile) throws IOException {
if (srx == null) {
if (!outFile.delete()) {
throw new RuntimeException();
}
return;
}
try {
srx.setVersion(CURRENT_VERSION);
XMLEncoder xmlenc = new XMLEncoder(new FileOutputStream(outFile));
xmlenc.writeObject(srx);
xmlenc.close();
} catch (IOException ioe) {
LOG.warn("IO Error", ioe);
throw ioe;
}
}
示例12: serializeObject
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public static boolean serializeObject(File file, Object o) {
if (file.exists()) {
System.out
.println("Warning! Object Already Exists \n Replacing File.");
}
try {
FileOutputStream os = new FileOutputStream(file);
XMLEncoder encoder = new XMLEncoder(os);
encoder.writeObject(o);
encoder.close();
} catch (Exception f) {
System.out.println("Warning! Write was Unsuccessful");
return false;
}
return true;
}
示例13: objectXmlEncoder
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* 将Object序列化到XML文件
* @param obj 目标对象
* @param fileName 完整路径的文件名
* @throws IOException
*/
public static void objectXmlEncoder(Object obj , String fileName) throws IOException{
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();
}
示例14: save
import java.beans.XMLEncoder; //导入方法依赖的package包/类
public void save() {
try {
Engine.logEngine.debug("(Scheduler Manager) Start jobs saving ...");
XMLEncoder encoder = new XMLEncoder(new FileOutputStream(getFileURL()));
encoder.writeObject(schedulerXML);
encoder.close();
Engine.logEngine.debug("(Scheduler Manager) ... jobs saving finished !");
} catch (FileNotFoundException e) {
Engine.logEngine.error("(Scheduler Manager) ... jobs saving failed !", e);
}
}
示例15: saveTemplates
import java.beans.XMLEncoder; //导入方法依赖的package包/类
/**
* Saves all templates as XML files in the current template directory.
*
* @return Whether or not the save was successful.
*/
public synchronized boolean saveTemplates() {
if (templates==null) {
return true;
}
if (directory==null || !directory.isDirectory()) {
return false;
}
// Blow away all old XML files to start anew, as some might be from
// templates we're removed from the template manager.
File[] oldXMLFiles = directory.listFiles(new XMLFileFilter());
if (oldXMLFiles==null) {
return false; // Either an IOException or it isn't a directory.
}
int count = oldXMLFiles.length;
for (int i=0; i<count; i++) {
/*boolean deleted = */oldXMLFiles[i].delete();
}
// Save all current templates as XML.
boolean wasSuccessful = true;
for (CodeTemplate template : templates) {
File xmlFile = new File(directory, template.getID() + ".xml");
try {
XMLEncoder e = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(xmlFile)));
e.writeObject(template);
e.close();
} catch (IOException ioe) {
ioe.printStackTrace();
wasSuccessful = false;
}
}
return wasSuccessful;
}