gpt4 book ai didi

java - 序列化对象仅打印最后一个

转载 作者:行者123 更新时间:2023-12-02 10:36:19 25 4
gpt4 key购买 nike

目前我面临这个问题:

我有一个对象(形状)的数组列表,当我尝试序列化它时,它只返回最后一个。

这是使用形状数组列表保存整个项目的按钮。

//Setting action listener from the "save" button
save.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
FileOutputStream out = null;
PrintWriter print = null;
String fName;
JFileChooser jfc1 = new JFileChooser();
jfc1.setAcceptAllFileFilterUsed(false);
jfc1.setFileFilter(xmlfilter);
jfc1.setDialogTitle("Enter the file's name to save");
int value = jfc1.showSaveDialog((JMenuItem)e.getSource());
if(value == JFileChooser.APPROVE_OPTION){
for(int i=0; i<images.size(); i++){
try{
fName = jfc1.getSelectedFile().getAbsolutePath();
if(!fName.endsWith(".xml")){
out = new FileOutputStream(fName + ".xml");
print = new PrintWriter(out);
}
else{
out = new FileOutputStream(fName);
print = new PrintWriter(out);
}
XStream xstream = new XStream(new DomDriver());
xstream.autodetectAnnotations(true);
String xml = xstream.toXML(images.get(i));
String auxTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
xml = xml.substring(xml.indexOf("</javax.swing.JPanel>"));
xml = xml.replace("</javax.swing.JPanel>", "");
xml = xml.replace("</classes.Circle>", "");
xml = xml.replace("</classes.Rectangle>", "");
xml = xml.replace("</classes.Line>", "");
auxTitle = auxTitle + xml;
System.out.println(auxTitle);
print.println(auxTitle);
print.flush();
}
catch(IOException ex){
JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
}
finally{
if(out != null){
try{
out.close();
}
catch(IOException exc){
JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
}
}
else if(print != null){
print.close();
}
}
}
}
}
});

例如,我画了 3 个形状(圆形、矩形和直线),控制台向我显示了以下输出:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
<height>104</height>
<id>0</id>
<idConnectedShape>0</idConnectedShape>
<numClick>0</numClick>
<width>122</width>
<begin>
<x>114</x>
<y>87</y>
</begin>
<color>
<red>0</red>
<green>0</green>
<blue>0</blue>
<alpha>255</alpha>
</color>
<end>
<x>236</x>
<y>191</y>
</end>
<entries>
<string>C:\\Users\\...\\Ferrari.jpg</string>
</entries>
<operator>ReadImage.</operator>
<output>&apos;img.mat&apos;</output>
<shape>Circle</shape>
</default>
</classes.Shape>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
<height>20</height>
<id>1</id>
<idConnectedShape>0</idConnectedShape>
<numClick>0</numClick>
<width>20</width>
<begin>
<x>75</x>
<y>139</y>
</begin>
<color>
<red>0</red>
<green>0</green>
<blue>0</blue>
<alpha>255</alpha>
</color>
<end>
<x>95</x>
<y>159</y>
</end>
<entries/>
<shape>Rectangle</shape>
</default>
</classes.Shape>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
<height>10</height>
<id>2</id>
<idConnectedShape>0</idConnectedShape>
<numClick>0</numClick>
<width>90</width>
<begin>
<x>85</x>
<y>149</y>
</begin>
<color>
<red>255</red>
<green>0</green>
<blue>0</blue>
<alpha>255</alpha>
</color>
<end>
<x>175</x>
<y>139</y>
</end>
<entries/>
<shape>Line</shape>
</default>
</classes.Shape>

创建的文件是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<classes.Shape>
<default>
<height>10</height>
<id>2</id>
<idConnectedShape>0</idConnectedShape>
<numClick>0</numClick>
<width>90</width>
<begin>
<x>85</x>
<y>149</y>
</begin>
<color>
<red>255</red>
<green>0</green>
<blue>0</blue>
<alpha>255</alpha>
</color>
<end>
<x>175</x>
<y>139</y>
</end>
<entries/>
<shape>Line</shape>
</default>
</classes.Shape>

我想强调的是,我已经尝试过使用其他 XML 序列化 API,例如 Xstream(我现在正在使用)、JAXB、简单 XML 序列化、java.beans.XMLDecoder。不幸的是,一切都失败了。

最佳答案

问题是,您在循环中创建了 FileOutputStream 和 PrintWriter。尝试类似的事情

...
try{
fName = jfc1.getSelectedFile().getAbsolutePath();
if(!fName.endsWith(".xml")){
out = new FileOutputStream(fName + ".xml");
print = new PrintWriter(out);
}
else{
out = new FileOutputStream(fName);
print = new PrintWriter(out);
}
for(int i=0; i<images.size(); i++){
XStream xstream = new XStream(new DomDriver());
xstream.autodetectAnnotations(true);
String xml = xstream.toXML(images.get(i));
String auxTitle = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n";
xml = xml.substring(xml.indexOf("</javax.swing.JPanel>"));
xml = xml.replace("</javax.swing.JPanel>", "");
xml = xml.replace("</classes.Circle>", "");
xml = xml.replace("</classes.Rectangle>", "");
xml = xml.replace("</classes.Line>", "");
auxTitle = auxTitle + xml;
System.out.println(auxTitle);
print.println(auxTitle);
print.flush();
}
}
catch(IOException ex){
JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
}
finally{
if(out != null){
try{
out.close();
}
catch(IOException exc){
JOptionPane.showMessageDialog(null, "Error creating the file! Please, try again!");
}
}
else if(print != null){
print.close();
}
}
...

关于java - 序列化对象仅打印最后一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53269028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com