Serializing objetcs prints only last one












0














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>&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>


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.










share|improve this question



























    0














    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>&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>


    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.










    share|improve this question

























      0












      0








      0







      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>&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>


      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.










      share|improve this question













      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>&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>


      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






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 19:42









      zsp

      186




      186
























          1 Answer
          1






          active

          oldest

          votes


















          1














          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();
          }
          }
          ...





          share|improve this answer























            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
            });


            }
            });














            draft saved

            draft discarded


















            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









            1














            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();
            }
            }
            ...





            share|improve this answer




























              1














              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();
              }
              }
              ...





              share|improve this answer


























                1












                1








                1






                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();
                }
                }
                ...





                share|improve this answer














                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();
                }
                }
                ...






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 12 at 22:16

























                answered Nov 12 at 22:11









                Wladimir Diskowski

                586




                586






























                    draft saved

                    draft discarded




















































                    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.




                    draft saved


                    draft discarded














                    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





















































                    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







                    Popular posts from this blog

                    Xamarin.iOS Cant Deploy on Iphone

                    Glorious Revolution

                    Dulmage-Mendelsohn matrix decomposition in Python