Serializing objetcs prints only last one
currently I am facing this problem:
I have an arraylist of objects (Shapes) and when I try to serialize it, it returns me only the last one.
Here is the button that saves the whole project with the arraylist of shapes.
//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();
}
}
}
}
}
});
For example, I drew 3 shapes (a circle, a rectangle and a line), and console showed me this output:
<?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>'img.mat'</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>
And the file created is:
<?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>
I would like to emphasize that have already tried to use another XML-serialization API's, like Xstream(I am using now), JAXB, Simple XML Serialization, java.beans.XMLDecoder. Unfortunately, all have failed.
java xml object serialization xml-serialization
add a comment |
currently I am facing this problem:
I have an arraylist of objects (Shapes) and when I try to serialize it, it returns me only the last one.
Here is the button that saves the whole project with the arraylist of shapes.
//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();
}
}
}
}
}
});
For example, I drew 3 shapes (a circle, a rectangle and a line), and console showed me this output:
<?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>'img.mat'</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>
And the file created is:
<?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>
I would like to emphasize that have already tried to use another XML-serialization API's, like Xstream(I am using now), JAXB, Simple XML Serialization, java.beans.XMLDecoder. Unfortunately, all have failed.
java xml object serialization xml-serialization
add a comment |
currently I am facing this problem:
I have an arraylist of objects (Shapes) and when I try to serialize it, it returns me only the last one.
Here is the button that saves the whole project with the arraylist of shapes.
//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();
}
}
}
}
}
});
For example, I drew 3 shapes (a circle, a rectangle and a line), and console showed me this output:
<?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>'img.mat'</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>
And the file created is:
<?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>
I would like to emphasize that have already tried to use another XML-serialization API's, like Xstream(I am using now), JAXB, Simple XML Serialization, java.beans.XMLDecoder. Unfortunately, all have failed.
java xml object serialization xml-serialization
currently I am facing this problem:
I have an arraylist of objects (Shapes) and when I try to serialize it, it returns me only the last one.
Here is the button that saves the whole project with the arraylist of shapes.
//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();
}
}
}
}
}
});
For example, I drew 3 shapes (a circle, a rectangle and a line), and console showed me this output:
<?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>'img.mat'</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>
And the file created is:
<?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>
I would like to emphasize that have already tried to use another XML-serialization API's, like Xstream(I am using now), JAXB, Simple XML Serialization, java.beans.XMLDecoder. Unfortunately, all have failed.
java xml object serialization xml-serialization
java xml object serialization xml-serialization
asked Nov 12 at 19:42
zsp
186
186
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Problem is, that you create FileOutputStream and PrintWriter in the loop.
try something like
...
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();
}
}
...
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269028%2fserializing-objetcs-prints-only-last-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Problem is, that you create FileOutputStream and PrintWriter in the loop.
try something like
...
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();
}
}
...
add a comment |
Problem is, that you create FileOutputStream and PrintWriter in the loop.
try something like
...
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();
}
}
...
add a comment |
Problem is, that you create FileOutputStream and PrintWriter in the loop.
try something like
...
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();
}
}
...
Problem is, that you create FileOutputStream and PrintWriter in the loop.
try something like
...
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();
}
}
...
edited Nov 12 at 22:16
answered Nov 12 at 22:11
Wladimir Diskowski
586
586
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53269028%2fserializing-objetcs-prints-only-last-one%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown