Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are getting an exception since data in file users.csv is stored as string whereas you are trying to convert (cast) it to HashSet . In general readObject() method returns an Object which can be casted to same data type which was serialized. Please check what object was added to file users.csv. Was it the ArrayList or something else.</p> <p>Note: User objects cannot be stored in file if it is not Serializable, you would get java.io.NotSerializableException</p> <p>Code below shows the sample User read and writes with some modification</p> <p><strong>User.java</strong> </p> <pre><code>package de.umass.lastfm; import java.io.Serializable; public class User implements Serializable{ private static final long serialVersionUID = 1L; private String id; private String name; /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } } </code></pre> <p><strong>UniqueUsersData.java</strong></p> <pre><code>package de.umass.lastfm; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FileWriter; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; public class UniqueUsersData { public static void main(String[] args) throws IOException, ClassNotFoundException{ //populating user object in arraylist STARTS User user1 = new User(); user1.setId("1"); user1.setName("name1"); User user2 = new User(); user2.setId("2"); user2.setName("name2"); List&lt;User&gt; userList = new ArrayList&lt;User&gt;(); userList.add(user1); userList.add(user2); //populating user object in arraylist ENDS // Add ArrayList&lt;User&gt; to file users.csv STARTS try{ FileOutputStream fout = new FileOutputStream("users.csv"); ObjectOutputStream oos = new ObjectOutputStream(fout); oos.writeObject(userList); oos.close(); System.out.println("file added users.csv"); }catch(Exception ex){ ex.printStackTrace(); } // Add ArrayList&lt;User&gt; to file users.csv ENDS // Reading ArrayList&lt;User&gt; from file users.csv STARTS ArrayList&lt;User&gt; userData = null; String fileName = "users.csv"; ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName)); try{ userData = (ArrayList&lt;User&gt;) in.readObject(); }catch(IOException e){ e.printStackTrace(); } finally { in.close(); } String file = "usersInfo.csv"; BufferedWriter out = new BufferedWriter(new FileWriter(file, true)); for(User u: userData){ System.out.println(u.getId() + "," + u.getName() + "\n"); } out.close(); } // end main method } // end class </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload