如何将文件集合写入文件? [英] How can I write a collection of objects to file?

查看:158
本文介绍了如何将文件集合写入文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试收集电影并将其保存到文件中。一切正常,直到我打开文本文件,我得到这个:



¬ísr?java.util.ArrayListxÒ™Ça我sizexp w sr?tema3ex1.movi​​e} ÆöôQ¹ªI idI ratingL categoryt Ljava / lang / String; L nameq〜L?releaseDateq〜xpý²t Sci-Fit Inceptiont
2010年7月30日



我搜索很多,找不到一种方法来使这项工作。而且我还得到这个可序列化的类不声明一个静态的final。
你能帮我吗?

  public class movie implements Serializable {

private字符串名称
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 ,2010年7月30日,8,1375666);
列表< movie> list = new ArrayList< movie>();
A.add(list,A);
A.save(list);

}


解决方案


¬ísr sr srÇÇÇÇÇÇie ie ieªªªªª你可以得到这样的输出,因为你是序列化你的对象。如果要将ArrayList的内容以可读格式保存到文件中,则需要使用其中一个writer API类(如FileWriter)写入内容。


我还得到这个可序列化的类不声明静态
final


< blockquote>

这是一个警告,而不是编译错误。向类中添加静态serialVersionUID的目的是提供一种方法来确保类在时间之间没有更改被序列化和反序列化,想象你串行化一个有2个字段的类,你运行你的应用程序,它将序列化你的对象..到目前为止这么好,现在你决定再添加一个字段到你的应用程序,反序列化您的序列化对象JVM如何知道您的类已更改?是的,它将序列化对象的serialVersionUID与类类型的serialVersionUID进行比较,如果发现ids不匹配,则异常将这个意思是,每当你改变一个可序列化的类时,你应该确保你更改serialVersionU的值ID字段,以保护您的应用程序不可理解的问题。请注意,虽然静态字段未被序列化,但serialVersionUID是一个特殊的标识符,其值被写入输出。


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

}

解决方案

"¬í 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.

这篇关于如何将文件集合写入文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆