简体   繁体   中英

How can I write a collection of objects to file?

I'm trying to make a collection of movies and save it to a file. Everything works fine until I open the text file and i get this:

"¬í sr ?java.util.ArrayListxÒ™Ça I sizexp w sr ?tema3ex1.movie}ÆöôQ¹ª I idI ratingL categoryt Ljava/lang/String;L nameq ~ L ?releaseDateq ~ xp ý² t Sci-Fit Inceptiont 30 July 2010x"

I searched a lot and couldn't find a way to make this work.And I also get "this serializable class does not declare a static final". Can you guys help me please?

    public class movie implements Serializable {

private String name;
private String category;
private String releaseDate;
private int rating;
private int id;

 public movie(String name, String category, String releaseDate, int rating, int id){
     this.name=name;
     this.category=category;
     this.releaseDate=releaseDate;
     this.rating=rating;
     this.id=id;

 }

 public void add(List<movie> list, movie A){
     list.add(A);       
}


public void delete(List<movie> list, movie A){
    list.remove(A);
}

public void save(List<movie> list) throws IOException{
    try {
        FileOutputStream fout = new FileOutputStream("tmp.txt");
        ObjectOutputStream out = new ObjectOutputStream(fout);
        out.writeObject(list);
        out.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) throws IOException{  
    movie A = new movie("Inception", "Sci-Fi", " 30 July 2010", 8, 1375666);
    List<movie> list = new ArrayList<movie>();
    A.add(list, A);
    A.save(list);

}

I think you want a file with human readable text. In order to achieve this, you cannot use the java serialization. You need to choose a format (like csv, xml, Json or whatever), and grab a serialization library to do it. In doing this, you can write a program in java which could read this file and convert it in object ( parsing the file). You can use Jackson CSV.

"¬í sr java.util.ArrayListxÒ™Ça I sizexp w sr tema3ex1.movie}ÆöôQ¹ª I idI ratingL categoryt Ljava/lang/String;L nameq ~ L releaseDateq ~ xp ý² t Sci-Fit Inceptiont 30 July 2010x"

You get this kind of output because you are serializing your object. If you want to save the contents of your ArrayList to the file in a readable format, you need to write the contents using one of the writer API classes such as FileWriter.

And I also get "this serializable class does not declare a static final

This is a warning and not a compilation error. The purpose of adding a static serialVersionUID to a class is to provide a way to ensure that the class did not change between the time it was serialized and deserialized. Imagine that you serialize a class which has 2 fields. You run your application, it serializes your object.. So far so good. Now you decide to add one more field to the class. You run your application and it deserializes your serialized object. How does the JVM know that your class has changed? That's right, it compares the serialVersionUID of the serialized object with the serialVersionUID of the class type. If it finds that the ids don't match, an exception will be thrown. The implication of this is that whenever you change a serializable class, you should make sure that you change the value of the serialVersionUID field to safeguard your application from incomprehensible issues.

Note that while static fields are not serialized, serialVersionUID is a special identifier who's value is writen to the output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM