这是我用于我的课程的代码。我已经测试了很多次,如果我不序列化和反序列化,这些功能都可以正常工作。
public class Library {
private String libTitle;
private Vector<Album> albums;
public String getLibTitle() {
return libTitle;
}
public void setLibTitle(String libTitle) {
this.libTitle = libTitle;
}
public Vector<Album> getAlbums() {
return albums;
}
public void setAlbums(Vector<Album> albums) {
this.albums = albums;
}
public Library(){
}
public Library(String libTitle) {
this.libTitle = libTitle;
this.albums = new Vector<Album>();
albums.trimToSize();
}
public void addAlbum(String album){
boolean added = false;
for (Album alb: this.getAlbums()){
if (alb.getAlbum()==album){
added=true;
}
if (added){
break;
}
}
if (!added){
this.getAlbums().add(new Album(album));
this.getAlbums().trimToSize();
}
}
public void removeAlbum(String album){
for (Album alb : this.getAlbums()){
if (alb.getAlbum()==album){
this.getAlbums().remove(alb);
this.getAlbums().trimToSize();
break;
}
}
}
public void addSong(String title, String author, String album){
boolean added = false;
for (Album alb : this.getAlbums()){
if (alb.getAlbum()==album){
alb.addSong(title,author);
added=true;
}
if (added){
break;
}
}
if (!added){
this.addAlbum(album);
this.addSong(title, author, album);
}
}
public void removeSong(String title, String author, String album){
boolean removed = false;
for (Album alb : this.getAlbums()){
if (alb.getAlbum()==album){
alb.removeSong(title);
if(alb.getSongs().isEmpty()){
this.getAlbums().remove(alb);
this.getAlbums().trimToSize();
};
removed=true;
}
if (removed){
break;
}
}
}
public void save()
{
try {
FileOutputStream xmlos = new FileOutputStream(this.libTitle +".xml");
XMLEncoder encoder = new XMLEncoder(xmlos);
encoder.writeObject(this);
encoder.close();
xmlos.close();
System.out.println("Done exporting a user as xml to "+this.libTitle+".xml");
}catch(Exception e) {
e.printStackTrace();
}
}
public Library restore(String lib)
{
Library newLib = null;
try {
System.out.println("Importing a user as xml from "+lib+".xml");
FileInputStream inFileStream = new FileInputStream(lib +".xml");
XMLDecoder decoder = new XMLDecoder(inFileStream);
newLib = (Library)decoder.readObject();
decoder.close();
inFileStream.close();
System.out.println("Libloaded "+ newLib.getLibTitle());
return newLib;
}catch(Exception e) {
e.printStackTrace();
}
return newLib;
}
}
接下来是相册类,在库类中使用
public class Album {
private String album;
private Vector<Song> songs;
public Vector<Song> getSongs() {
return songs;
}
public void setSongs(Vector<Song> songs) {
this.songs = songs;
}
public Album(){
}
public Album(String album) {
this.album = album;
this.songs = new Vector<Song>();
songs.trimToSize();
}
public String getAlbum() {
return album;
}
public void setAlbum(String album) {
this.album = album;
}
public void addSong(String song, String author){
boolean added = false;
for (Song son : this.getSongs()){
if (son.getTitle()==song){
added=true;
}
if (added){
break;
}
}
if (!added){
this.getSongs().add(new Song(song, author));
this.getSongs().trimToSize();
}
}
public void removeSong(String song){
for (Song son : this.getSongs()){
if (son.getTitle()==song){
this.getSongs().remove(son);
this.getSongs().trimToSize();
break;
}
}
}
}
之后是歌曲类。
public class Song {
private String title,author;
public Song(){
}
public Song(String title, String author) {
this.title = title;
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
所以基本上它是一个库,我正在序列化为 XML,然后反序列化我正在制作的应用程序。我的问题是当我运行以下代码时......
public class Run {
public static void main(String args[]){
Library lib = new Library("MYLibrary");
lib.addSong("Crawling", "Lincoln Park", "Hybrid Theory");
lib.addSong("In The End", "Lincoln Park", "Hybrid Theory");
lib.addSong("Fire", "Pyros", "Burning Up");
lib.addSong("Ocean", "Drowners", "Burning Up");
lib.save();
Library lib2 = new Library();
lib2 =lib2.restore("MYLibrary");
lib2.setLibTitle("test");
lib2.removeSong("In The End", "Lincoln Park", "Hybrid Theory");
lib2.addSong("Crawling in the dark", "me", "Hybrid Theory");
lib2.removeSong("Crawling in the dark", "me", "Hybrid Theory");
lib2.removeSong("Ocean", "Drowners", "Burning Up");
lib2.removeAlbum("Hybrid Theory");
lib2.save();
}
}
保存的 XML 文件不会只有一首歌曲,因为它应该生成的两个 XML 文件是相同的。我的老师不明白为什么它不起作用,我也不明白。为什么图书馆不改变?