PlayList类和Song类都已经继承了序列化接口啊:
public class PlayList implements Serializable
public class Song implements Serializable
主程序中的代码:
case 8:
System.out.println("导出歌单 ");
System.out.println("请输入要导出的歌曲列表名称: ");
instr = scanner.next();
String fileName = instr + "的歌单.txt";
if (playListCollection.searchPlayListByName(instr) != null) {
PlayList sPlayList = playListCollection.searchPlayListByName(instr);
try {
FileOutputStream fos = new FileOutputStream(fileName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream(fileName);
ObjectInputStream ois = new ObjectInputStream(fis);
// 写入对象
// oos.writeObject(sPlayList);
Iterator<Song> it = sPlayList.getMusicList().iterator();
while (it.hasNext()) {
oos.writeObject(it.next());
}
oos.flush();
// 读对象信息
try {
PlayList rPlayList = (PlayList) ois.readObject();
System.out.println(rPlayList);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
oos.close();
fos.close();
ois.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
PlayListMenu();
break;
相关分类