导出歌单不知道错在哪
public class Song { private String id, name, singer; public Song() { } public Song(String id, String name, String singer) { super(); this.id = id; this.name = name; this.singer = singer; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSinger() { return singer; } public void setSinger(String singer) { this.singer = singer; } @Override public String toString() { return "歌曲 [id为:" + id + ", 名称为:" + name + ", 演唱者为:" + singer + "]"; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((id == null) ? 0 : id.hashCode()); result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + ((singer == null) ? 0 : singer.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj.getClass() == Song.class) { Song song = (Song) obj; return (song.getId().equals(id) && song.getName().equals(name) && song.getSinger().equals(singer)); } return false; } }
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.List; public class PlayList { private String playListName; private List<Song> musicList; public PlayList() { } /** * 构造方法 * * @param playListName 播放列表的名字 */ public PlayList(String playListName) { super(); this.setPlayListName(playListName); musicList = new ArrayList<Song>(); } public String getPlayListName() { return playListName; } public void setPlayListName(String playListName) { this.playListName = playListName; } public List<Song> getMusicList() { return musicList; } public void setMusicList(List<Song> musicList) { this.musicList = musicList; } /** * 方法一:增 * * @param song 歌曲类对象 */ public void addTo(Song song) { boolean flag = false; for (Song s : musicList) { if (s.equals(song)) { flag = true; break; } } if (flag) { System.out.println("歌曲已经存在,添加失败!"); } else { musicList.add(song); } } /** * 方法二:显 */ public void display() { System.out.println("所有歌曲为:"); for (Song s : musicList) { System.out.println(s); } } /** * 方法三:查 * * @param name 歌曲的名字 * @return 查到的歌曲信息,Song类的 */ public Song searchSongByName(String name) { Song song = null; for (Song s : musicList) { if (s.getName().equals(name)) { song = s; break; } } return song; } /** * 方法三:查 * * @param id 歌曲的id * @return 查到的歌曲信息,Song类的 */ public Song searchSongById(String id) { Song song = null; for (Song s : musicList) { if (s.getId().equals(id)) { song = s; break; } } return song; } /** * 改 * * @param id 要查的歌曲的id * @param song 新的歌曲信息 */ public void update(String id, Song song) { Song songUpdate = searchSongById(id); if (songUpdate == null) { System.out.println("没有找到:" + id + "对应的歌曲"); } else { musicList.remove(songUpdate); musicList.add(song); } } /** * 删 * * @param id 指定删除歌曲的id */ public void delete(String id) { Song songDelete = searchSongById(id); if (songDelete == null) { System.out.println("没有找到id为:" + id + "对应的歌曲信息!请先添加!"); } else { musicList.remove(songDelete); } } }
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.HashMap; import java.util.Map; import java.util.Set; public class PlayListCollection { Map<String, PlayList> playListMap; public PlayListCollection() { playListMap = new HashMap<String, PlayList>(); } public Map<String, PlayList> getPlayListMap() { return playListMap; } public void setPlayListMap(Map<String, PlayList> playListMap) { this.playListMap = playListMap; } /** * 方法一:增 * * @param playList 要增加的PlayList对象 */ public void addPlayList(PlayList playList) { playListMap.put(playList.getPlayListName(), playList); } /** * 方式二: 删 * * @param playList */ public void deletePlayList(PlayList playList) { playListMap.remove(playList.getPlayListName()); System.out.println("删除成功!"); } /** * 方式三:查 * * @param playListName * @return 播放列表 */ public PlayList searchPlayListByName(String playListName) { PlayList playList = null; Set<String> set = playListMap.keySet(); for (String s : set) { if (s.equals(playListName)) { playList = playListMap.get(s); break; } } return playList; } /** * 方式四:显 */ public void displayAllList() { Set<String> set = playListMap.keySet(); System.out.println("播放器中播放列表名称为:"); for (String s : set) { System.out.println(s); } } /** * 方式五:导出歌单信息 * * @param playListName 歌单的名称 * @throws IOException 输入输出异常 */ public void outPut(String playListName) throws IOException { FileOutputStream fos = null; ObjectOutputStream oos = null; FileInputStream fis = null; ObjectInputStream ois = null; try { // 写入 fos = new FileOutputStream("名称.txt"); oos = new ObjectOutputStream(fos); // 读出 fis = new FileInputStream("名称.txt"); ois = new ObjectInputStream(fis); PlayList playListSearch = searchPlayListByName(playListName); if (playListSearch == null) { System.out.println("该歌单列表不存在,请先添加!"); } else { oos.writeObject(playListSearch); oos.flush(); Object obj = null; try { while ((obj = ois.readObject()) != null) { System.out.println(obj); System.out.println(); } } catch (ClassNotFoundException e) { e.printStackTrace(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { fos.close(); oos.close(); fis.close(); ois.close(); } } }
import java.io.IOException; import java.util.Scanner; public class TestDemo { // 主菜单页面 public void mainMenu() { System.out.println("***************************************"); System.out.println(" **主菜单** "); System.out.println(" 1--播放列表管理 "); System.out.println(" 2--播放器管理 "); System.out.println(" 0--退出 "); System.out.println("***************************************"); } // 播放列表管理菜单页面 public void playListMenu() { System.out.println("***********************************************"); System.out.println(" **播放列表管理** "); System.out.println(" 1--将歌曲添加到主播放列表 "); System.out.println(" 2--将歌曲添加到普通播放列表 "); System.out.println(" 3--通过歌曲名称查询播放列表中的歌曲 "); System.out.println(" 4--通过歌曲id查询并修改播放列表中的歌曲 "); System.out.println(" 5--通过歌曲id查询并删除所有播放列表中的当前歌曲 "); System.out.println(" 6--显示播放列表中的所有歌曲 "); System.out.println(" 9--返回上一级菜单 "); System.out.println("***********************************************"); } // 播放器菜单页面 public void playerMenu() { System.out.println("***********************************************"); System.out.println(" **播放器管理** "); System.out.println(" 1--向播放器添加播放列表 "); System.out.println(" 2--从播放器删除播放列表 "); System.out.println(" 3--通过名字查询播放列表信息 "); System.out.println(" 4--显示所有播放列表名称 "); System.out.println(" 8--导出歌单 "); System.out.println(" 9--返回上一级菜单 "); System.out.println("***********************************************"); } public void test() { TestDemo td = new TestDemo(); Scanner sc = new Scanner(System.in); int input1, input2, input3; // 创建一个播放器 PlayListCollection plc = new PlayListCollection(); // 创建一个主播放列表 PlayList mainPL = new PlayList("主播放列表"); // 将主播放列表放入播放器 plc.addPlayList(mainPL); PlayList diyList = null; while (true) { td.mainMenu(); System.out.println("请输入对应的数字对播放列表进行管理:"); input1 = sc.nextInt(); if (input1 == 0) { System.out.println("退出程序!"); break; } switch (input1) { // 播放列表管理 case 1: while (true) { td.playListMenu(); System.out.println("请输入数字对播放列表进行管理:"); input2 = sc.nextInt(); if (input2 == 9) { break; } switch (input2) { case 1: System.out.println("请输入要添加的歌曲数量"); int count = sc.nextInt(); for (int i = 1; i <= count; i++) { // 从键盘给歌曲赋值 System.out.println("请输入第:" + i + "首歌"); System.out.println("请输入歌曲id:"); String strId = sc.next(); System.out.println("请输入歌曲名称:"); String strName = sc.next(); System.out.println("请输入演唱者:"); String strSinger = sc.next(); Song song = new Song(strId, strName, strSinger); mainPL.addTo(song); mainPL.display(); } break; case 2: System.out.println("请输入要添加的播放列表名称"); String strPlayList = sc.next(); // 判断播放列表是否已经存在 diyList = plc.searchPlayListByName(strPlayList); if (diyList == null) { System.out.println("该播放列表不存在,请先添加!"); break; } else { System.out.println("请输入要添加的歌曲数量:"); int count1 = sc.nextInt(); for (int i = 1; i <= count1; i++) { System.out.println("请输入要添加的第:" + i + "首歌的信息:"); System.out.println("请输入要添加的歌曲id:"); String strId = sc.next(); Song songSearch = mainPL.searchSongById(strId); if (songSearch == null) { // 如果不存在则双添加 System.out.println("该歌曲在主播放列表不存在,继续输入歌曲的其他信息!"); System.out.println("请输入要添加的歌曲名称:"); String strName = sc.next(); System.out.println("请输入要添加的演唱者信息:"); String strSinger = sc.next(); songSearch = new Song(strId, strName, strSinger); diyList.addTo(songSearch); mainPL.addTo(songSearch); } else { // 如果存在则添加到自定义 diyList.addTo(songSearch); System.out.println("添加成功!"); } System.out.println("主播放列表:"); mainPL.display(); System.out.println("自定义播放列表:"); diyList.display(); } } break; case 3: System.out.println("请输入要查询的播放列表名称:"); String strPlayListName = sc.next(); diyList = plc.searchPlayListByName(strPlayListName); if (diyList == null) { // 没有的话跳出 System.out.println("该播放列表不存在,请先创建!"); break; } else { // 有的话输出查询结果 System.out.println("请输入要查询的歌的名称:"); String strName = sc.next(); Song songSearch = diyList.searchSongByName(strName); if (songSearch == null) { System.out.println("该歌曲在播放列表:" + strPlayListName + "中不存在"); } else { System.out.println("歌曲信息为:"); System.out.println(songSearch); } } break; case 4: System.out.println("请输入要查询歌曲所在的播放列表:"); String strPlayListName1 = sc.next(); diyList = plc.searchPlayListByName(strPlayListName1); if (diyList == null) { System.out.println("该播放列表不存在,请先创建!"); break; } else { System.out.println("请输入要查询歌曲的id:"); String strId = sc.next(); Song songSearch = diyList.searchSongById(strId); if (songSearch == null) { System.out.println("该歌曲在播放列表:" + strPlayListName1 + "中不存在"); } else { System.out.println("请修改:"); System.out.println("请输入要修改的歌曲新id:"); String strIdNew = sc.next(); System.out.println("请输入要修该的歌曲新名称:"); String strNameNew = sc.next(); System.out.println("请输入要修改的歌曲新演唱者:"); String strSingerNew = sc.next(); Song songNew = new Song(strIdNew, strNameNew, strSingerNew); diyList.update(strId, songNew); mainPL.update(strId, songNew); System.out.println("修改成功!"); System.out.println("主播放列表:"); mainPL.display(); System.out.println("自定义播放列表:"); diyList.display(); } } break; case 5: System.out.println("请输入要查询歌曲所在的播放列表:"); String strPlayListName2 = sc.next(); diyList = plc.searchPlayListByName(strPlayListName2); if (diyList == null) { System.out.println("该播放列表不存在,请先创建!"); break; } else { System.out.println("请输入要查询歌曲的id:"); String strId = sc.next(); Song songSearch = diyList.searchSongById(strId); if (songSearch == null) { System.out.println("该歌曲在播放列表:" + strPlayListName2 + "中不存在"); } else { diyList.delete(strId); mainPL.delete(strId); System.out.println("删除成功!"); System.out.println("主播放列表:"); mainPL.display(); System.out.println("自定义播放列表:"); diyList.display(); } } break; case 6: System.out.println("主播放列表:"); mainPL.display(); System.out.println("自定义播放列表:"); diyList.display(); break; default: System.out.println("该数字没有对应的操作!"); break; } } break; // 播放器管理 case 2: while (true) { td.playerMenu(); System.out.println("请输入对应的数字对播放器进行管理"); input3 = sc.nextInt(); if (input3 == 9) { break; } switch (input3) { case 1: System.out.println("请输入要添加的播放列表名称:"); String strPlayListName = sc.next(); // 创建自定义对象 diyList = new PlayList(strPlayListName); // 添加到HashMap中 plc.addPlayList(diyList); System.out.println("添加成功!"); break; case 2: System.out.println("请输入要删除的播放列表名称:"); String strPlayListName1 = sc.next(); if (strPlayListName1.equals("主播放列表")) { System.out.println("主播放列表不能删除!"); break; } else { PlayList plSearch = plc.searchPlayListByName(strPlayListName1); if (plSearch == null) { System.out.println("该播放列表不存在,请先创建"); } else { plc.deletePlayList(plSearch); plc.displayAllList(); } } break; case 3: System.out.println("请输入要查询的播放列表名称:"); String strPlayListName2 = sc.next(); PlayList plSearch = plc.searchPlayListByName(strPlayListName2); if (plSearch == null) { System.out.println("该播放列表不存在,请先创建"); } else { System.out.println("该播放列表存在!"); System.out.println("该播放列表名称为:" + strPlayListName2); plSearch.display(); } break; case 4: System.out.println("所有播放列表的名称为:"); plc.displayAllList(); break; case 8: // 导出歌单 System.out.println("请输入要导出歌单的名称:"); String strPlayListName3 = sc.next(); PlayList plSearch1 = plc.searchPlayListByName(strPlayListName3); if (plSearch1 == null) { System.out.println("该播放列表不存在,请先创建"); } else { System.out.println("该播放列表存在!"); try { plc.outPut(strPlayListName3); } catch (IOException e) { e.printStackTrace(); } } break; default: System.out.println("该数字没有对应的操作!"); break; } } break; default: System.out.println("没有对应数字操作!"); break; } } } public static void main(String[] args) { TestDemo td = new TestDemo(); td.test(); } }
不知道导出歌单错在哪里
23
收起
正在回答
3回答
同学你好,是报java.io.EOFException异常吗?这个异常是意外读到了末尾造成的报错,同学在使用oos.writeObject()写入时,可以加上oos.writeObject(null);表示到了文件末尾,如:
这样读取时,读取到null就表示到了文件末尾,不再循环读取,就不再报错。
同学可以给PlayList类加上toString()方法,导出成功并再次读取,结果如下:
祝:学习愉快~
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星