title: How can I XML serialize a synchronized ArrayList?
body: <p>When I try to serialize and deserialize an <code>ArrayList</code> wrapped in <code>Collections.synchronizedList</code> using <code>beans.XMLEncoder</code> and <code>beans.XMLDecoder</code>, I get the following error:</p>
  
  <pre><code>java.lang.reflect.InvocationTargetException  
  Continuing ...  
  java.lang.Exception: XMLEncoder: discarding statement XMLEncoder.writeObject*Collections$SynchronizedRandomAccessList);  
  Continuing ... 
  </code></pre>
  
  <p>Since the program I am working on is a multithreaded music library client/server application, I need the synchronization. If using an ordinary ArrayList, the serialization/deserialization works fine. I really don't want to use Vector, since it contains a lot of legacy operations.</p>
  
  <p>Here are my methods to serialize and deserialize:</p>
  
  <pre><code>/**  
  * Serializes library into an XML file  
  * @param xmlFileLocation - location of XML file  
  */ 
  
  public void saveLibrary (String xmlFileLocation) {  
      FileOutputStream fos;  
      try {  
          fos = new FileOutputStream(xmlFileLocation);  
          XMLEncoder encoder = new XMLEncoder(fos);  
          encoder.writeObject(lib);  
          encoder.close();  
          fos.close();  
      } catch (Exception e) {  
      e.printStackTrace();  
      }  
  } 
  
  /**  
  * Constructor for Library, deserializes XML file  
  * @param xmlFileLocation - location of XML file  
  */ 
  @SuppressWarnings("unchecked")  
  public Library(String xmlFileLocation) {  
      FileInputStream fis;  
      try {  
          fis = new FileInputStream(xmlFileLocation);  
          XMLDecoder decoder = new XMLDecoder(fis);  
          Object o = decoder.readObject();  
          if (o instanceof List)    
              setLib((List&lt;MusicDescription&gt;) o);   
              decoder.close();  
              fis.close();  
      } catch (Exception e) {  
          e.printStackTrace();  
      }     
  } 
  </code></pre>
  
  <p>As I stated, I really don't want to use Vector, since it contains a lot of legacy operations.</p>