老师,为什么我导出歌单信息会有错误,而且two.txt也是乱码

老师,为什么我导出歌单信息会有错误,而且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();
    }
}

http://img1.sycdn.imooc.com//climg/5d19ee280001c57105940515.jpg

http://img1.sycdn.imooc.com//climg/5d19ee3700016e4805820467.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

2回答
好帮手慕小班 2019-07-04 19:54:41

        同学你好,复制运行贴出代码,读取不成功有如下原因:

    1、读取的文件名,不正确哦!

    2、读取方法的使用,是老师描述不准确,读取对象的方法是readObject();哦

    3、因为存入的对象不是只有一个,所以这里建议存入集合哦,在取的时候,也可以直接取出集合哦!  例如:

http://img1.sycdn.imooc.com//climg/5d1de8e7000108bb09160683.jpg

运行效果如下:

http://img1.sycdn.imooc.com//climg/5d1de8f90001232f05460187.jpg

        如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~


好帮手慕小班 2019-07-02 14:12:27

        同学你好,1、复制运行贴出代码,这里导出错误是因为,写入文件的是一个Song类型的对象,但是对象没有序列化,文件流就不知道该怎样写入这个对象文件,解决办法就是让Song类序列化一下就可以了哦!

http://img1.sycdn.imooc.com//climg/5d1af2130001a23e06590323.jpg

    2、关于写入文件是乱码,这里并不是乱码,而是存入的对象序列化后的内容,在老师的案例课程中同样存入的这些对象内容哦,比如课程中存入的内容

http://img1.sycdn.imooc.com//climg/5d1af5670001009112210441.jpg

而复制贴出代码,序列化对象后,如下的运行效果

http://img1.sycdn.imooc.com//climg/5d1af62b0001933b13290098.jpg

所以这里是正确的对象内容哦!同学可以将txt文件点击另存为->编码格式选择“UTF-8”哦。

          如果我的回答解决了你的疑惑,请采纳。祝:学习愉快~


  • 提问者 qq_就是这样_1 #1
    那老师如何才能让它导出后正常显示呢?
    2019-07-02 19:24:52
  • 同学可以将txt文件点击另存为->编码格式选择“UTF-8”试试。祝:学习愉快~
    2019-07-03 11:17:36
  • 提问者 qq_就是这样_1 #3
    那老师如何才能让它导出后正常显示呢?
    2019-07-03 19:07:25
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师
插入代码