老师,为什么我导出歌单信息会有错误,而且two.txt也是乱码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | package com.song.test; /** * 歌曲类 * @author Administrator * */ public class Song { private String ID; //歌曲ID private String name; //歌曲名 private String singer; //歌手名字 public Song(String ID,String name,String singer){ this .ID=ID; this .name=name; this .singer=singer; } public String getID() { return ID; } public void setID(String iD) { 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 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 ; } @Override public String toString() { return "歌曲信息:ID为" + ID + ", 歌名为" + name + ", 歌手为" + singer; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | package com.song.test; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; import java.io.Serializable; import java.util.ArrayList; import java.util.List; public class PlayList implements Serializable{ private String playListName; //播放列表名称 private List<Song> musicList; //播放列表中歌曲集合 /** * 构造方法 * @param playListName 播放列表名称 */ public PlayList(String playListName){ this .playListName=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 addToPlayList(Song song){ boolean flag= false ; for (Song song1:musicList){ if (song1.equals(song)){ flag= true ; break ; } } if (flag){ System.out.println( "该歌曲已经存在于播放列表中,添加失败!" ); } else { musicList.add(song); } } //显示播放列表中所有歌曲 public void displayAllSong(){ System.out.println( "播放列表中的所有歌曲为:" ); for (Song song:musicList){ System.out.println(song); } } /** * 通过id查询歌曲 * @param id歌曲的id * @return查询到的歌曲信息 */ public Song searchSongById(String id){ Song song= null ; for (Song song1:musicList){ if (song1.getID().equals(id)){ song=song1; break ; } } return song; } /** * 根据歌曲的名称查询 * @param name歌曲的名称 * @return查询到的歌曲信息 */ public Song searchSongByName(String name){ Song song= null ; for (Song song1:musicList){ if (song1.getName().equals(name)){ song=song1; break ; } } return song; } /** * 修改播放列表中的歌曲信息 * @param id 要修改的歌曲id * @param song 新的歌曲信息 */ public void updateSong(String id,Song song){ Song song1=searchSongById(id); if (song1== null ){ System.out.println( "找不到对应id为" +id+ "对应的歌曲信息!" ); } else { musicList.remove(song1); musicList.add(song); System.out.println( "修改成功!" ); } } /** * 删除播放列表中的指定歌曲信息 * @param id 要删除的歌曲id */ public void deleteSong(String id){ Song song2=searchSongById(id); if (song2== null ){ System.out.println( "找不到对应id为" +id+ "对应的歌曲信息!" ); } else { musicList.remove(song2); System.out.println( "删除成功!" ); } } public void exportPlayList(){ try { FileOutputStream fio= new FileOutputStream(playListName+ "two.txt" ); ObjectOutputStream oos = new ObjectOutputStream(fio); List<Song> songList=getMusicList(); for (Song song:songList){ oos.writeObject(song); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | package com.song.test; import java.util.HashMap; import java.util.Map; import java.util.Set; /** * 播放列表集合 * @author Administrator * */ public class PlayListCollection { Map<String,PlayList> playListMap; public PlayListCollection(){ playListMap= new HashMap<String,PlayList>(); } /** * 向播放列表集合(播放器)添加播放列表 * @param playList 要添加的播放列表 */ public void addPlayList(PlayList playList){ //播放列表名称作为key值 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> playListSet=playListMap.keySet(); for (String s:playListSet){ if (s.equals(playListName)){ playList=playListMap.get(s); break ; } } return playList; } /** * 显示所有播放列表的名称 */ public void displayListName(){ Set<String> playListSet=playListMap.keySet(); System.out.println( "播放列表名称为:" ); for (String s:playListSet){ System.out.println(s); } } public Map<String, PlayList> getPlayListMap() { return playListMap; } public void setPlayListMap(Map<String, PlayList> playListMap) { this .playListMap = playListMap; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 | package com.song.test; 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.Scanner; public class Test { // 主菜单 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--通过歌曲id查询播放列表中的歌曲 " ); System.out.println( "4--通过歌曲名称查询播放列表中的歌曲 " ); System.out.println( "5--修改播放列表中的歌曲 " ); System.out.println( "6--删除播放列表中的歌曲 " ); System.out.println( "7--显示播放列表中的所有歌曲 " ); System.out.println( "8--导出歌单信息 " ); System.out.println( "9--返回上一级菜单 " ); System.out.println( "**************************************" ); } // 播放器管理 public void player() { 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( "9--返回上一级菜单 " ); System.out.println( "**************************************" ); } // 主流程 public void test() { Test test = new Test(); Scanner sc = new Scanner(System.in); int input = 0 , input1 = 1 , input2 = 2 ; // 创建一个播放器 PlayListCollection plc = new PlayListCollection(); // 创建主播放列表 PlayList mainPlayList = new PlayList( "创建主播放列表" ); // 将主播放列表添加到播放器 plc.addPlayList(mainPlayList); PlayList favouritePlayList = null ; while ( true ) { test.mainMenu(); System.out.println( "请输入相应的数字进行操作:" ); input = sc.nextInt(); if (input == 0 ) { break ; } switch (input) { case 1 : // 播放列表管理 while ( true ) { test.playListMenu(); System.out.println( "请输入对应的数字对播放列表进行管理:" ); input1 = sc.nextInt(); if (input1 == 9 ) break ; switch (input1) { case 1 : System.out.println( "将歌曲添加 到主播放列表" ); 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); mainPlayList.addToPlayList(song); // mainPlayList.displayAllSong(); } break ; case 2 : System.out.println( "将歌曲添加 到普通播放列表" ); System.out.println( "请输入要添加的播放列表名称:" ); String sName = sc.next(); // 根据名称判断播放列表是否在播放器中存在 favouritePlayList = plc.searchPlayListByName(sName); if (favouritePlayList == null ) { System.out.println( "该播放列表不存在,请先将播放列表添加到播放器中!" ); } 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(); // 首先判断id的歌曲是否在主播放列表中存在 Song song = mainPlayList.searchSongById(strId); if (song == null ) { System.out.println( "该歌曲在主播放列表中不存在,继续输入歌曲的其他信息" ); System.out.println( "请输入歌曲的名称:" ); String strName = sc.next(); System.out.println( "请输入演唱者:" ); String strSinger = sc.next(); // 创建一个Song类的对象 song = new Song(strId, strName, strSinger); // 分别将歌曲添加普通播放列表和主播放列表 favouritePlayList.addToPlayList(song); mainPlayList.addToPlayList(song); } else { // 如果歌曲存在于主播放列表,则直接添加到现在的播放列表 favouritePlayList.addToPlayList(song); } } // 显示播放列表中的歌曲信息 System.out.println( "主播放列表" ); mainPlayList.displayAllSong(); System.out.println( "普通播放列表" ); favouritePlayList.displayAllSong(); } break ; case 3 : System.out.println( "通过歌曲id查询播放列表中的歌曲 " ); System.out.println( "请输入要查询的播放列表名称: " ); String strPlayListName1 = sc.next(); // 查询播放列表是否存在 PlayList pl = plc.searchPlayListByName(strPlayListName1); if (pl == null ) { System.out.println( "该播放列表不存在!" ); break ; } else { System.out.println( "请输入要查询的歌曲id:" ); String strId1 = sc.next(); Song s = pl.searchSongById(strId1); if (s == null ) { System.out.println( "该歌曲在播放列表" + strPlayListName1 + "中不存在!" ); } else { System.out.println( "该歌曲的信息为:" ); System.out.println(s); } } break ; case 4 : System.out.println( "通过歌曲名称查询播放列表中的歌曲 " ); System.out.println( "请输入要查询的播放列表名称: " ); String strPlayListName2 = sc.next(); PlayList p2 = plc.searchPlayListByName(strPlayListName2); if (p2 == null ) { System.out.println( "该播放列表不存在!" ); break ; } else { System.out.println( "请输入要查询的歌曲名称:" ); String strName = sc.next(); Song s = p2.searchSongByName(strName); if (s == null ) { System.out.println( "该歌曲在播放列表" + strPlayListName2 + "中不存在!" ); } else { System.out.println( "该歌曲的信息为:" ); System.out.println(s); } } break ; case 5 : System.out.println( "修改播放列表中的歌曲" ); System.out.println( "请输入要修改的播放列表名称" ); String strPlayListName3 = sc.next(); PlayList p3 = plc.searchPlayListByName(strPlayListName3); if (p3 == null ) { System.out.println( "该播放列表不存在!" ); break ; } else { System.out.println( "请输入要修改的歌曲id:" ); String strId3 = sc.next(); Song s = p3.searchSongById(strId3); if (s == null ) { System.out.println( "该歌曲在播放列表" + strPlayListName3 + "中不存在!" ); } else { System.out.println( "修改前的歌曲的名称:" + "\n" + s.getName()); System.out.println( "修改前的歌曲的演唱者:" + "\n" + s.getSinger()); System.out.println( "修改后的歌曲的名称:" ); String name = sc.next(); s.setName(name); System.out.println( "修改后的歌曲的演唱者:" ); String singer = sc.next(); s.setSinger(singer); } } break ; case 6 : System.out.println( "删除播放列表中的歌曲" ); System.out.println( "请输入要删除的歌曲的所在播放列表:" ); String strPlayListname4 = sc.next(); PlayList p4 = plc.searchPlayListByName(strPlayListname4); if (p4 == null ) { System.out.println( "该播放列表不存在!" ); } else { System.out.println( "请输入要删除的歌曲id:" ); String strId4 = sc.next(); Song s = p4.searchSongById(strId4); if (s == null ) { System.out.println( "该歌曲在播放列表" + strPlayListname4 + "中不存在!" ); } else { System.out.println( "删除的歌曲id:" ); p4.deleteSong(strId4); System.out.println( "删除成功!" ); } } break ; case 7 : System.out.println( "显示播放列表中的所有歌曲" ); System.out.println( "请输入要显示所有歌曲的播放列表名称:" ); String strPlayListname5 = sc.next(); PlayList p5 = plc.searchPlayListByName(strPlayListname5); if (p5 == null ) { System.out.println( "该播放列表不存在!" ); } else { p5.displayAllSong(); } break ; case 8 : // 导出歌单 System.out.println( "请输入要导出的播放列表名称:" ); String strPlayListname6 = sc.next(); PlayList p6 = plc.searchPlayListByName(strPlayListname6); //p6.getMusicList(); if (p6 == null ) { System.out.println( "该播放列表不存在!" ); } else { p6.exportPlayList(); System.out.println( "歌单导出成功!" ); } break ; default : System.out.println( "该数字没有对应的操作!" ); break ; } } break ; case 2 : // 播放器管理 while ( true ) { test.player(); System.out.println( "请输入对应的数字对播放器进行管理:" ); input2 = sc.nextInt(); if (input2 == 9 ) break ; switch (input2) { case 1 : System.out.println( "向播放器添加播放列表" ); System.out.println( "输入要添加的播放列表名称:" ); String playerName = sc.next(); // 创建一个新播放列表对象 favouritePlayList = new PlayList(playerName); // 将播放列表添加到播放器Map plc.addPlayList(favouritePlayList); break ; case 2 : System.out.println( "从播放器删除播放列表" ); System.out.println( "请输入要删除的播放列表" ); String strPlayListName = sc.next(); if (strPlayListName.equals( "主播放列表" )) { System.out.println( "主播放列表不能删除" ); break ; } // 查询播放列表是否存在 PlayList playList1 = plc.searchPlayListByName(strPlayListName); if (playList1 == null ) { System.out.println( "该播放列表不存在!" ); } else { // 存在则删除 plc.deletePlayList(playList1); } break ; case 3 : System.out.println( "通过名字查询播放列表信息" ); System.out.println( "请输入要查询的播放列表名称:" ); String strPlayList1 = sc.next(); PlayList playList2 = plc.searchPlayListByName(strPlayList1); if (playList2 == null ) { System.out.println( "该播放列表不存在!" ); } else { // 显示该播放列表名称及其中的所有歌曲 System.out.println( "该播放列表存在!" ); System.out.println( "该播放列表名称为:" + strPlayList1); playList2.displayAllSong(); } break ; case 4 : System.out.println( "显示所有播放列表名称" ); plc.displayListName(); break ; default : System.out.println( "该数字没有对应的操作!" ); break ; } } break ; default : System.out.println( "该数字没有对应的操作!" ); break ; } } mainPlayList.displayAllSong(); } public static void main(String[] args) { // TODO Auto-generated method stub Test testone = new Test(); // test.testsong(); // test.testPlayList(); // test.testPlayListCollection(); testone.test(); } } |
5
收起
正在回答 回答被采纳积分+1
2回答
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧