Repository navigation
Expand file tree
/
Copy pathPlaylistManager.java
More file actions
219 lines (149 loc) · 5.43 KB
/
Copy pathPlaylistManager.java
File metadata and controls
219 lines (149 loc) · 5.43 KB
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
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
class PlaylistManager {
// Read list of playlists from playlists.txt
public ArrayList<String> readPlaylists() {
ArrayList<String> lists = new ArrayList<>();
try {
BufferedReader br =
new BufferedReader(
new FileReader("playlists.txt"));
String line;
while ((line = br.readLine()) != null) {
if (!line.trim().isEmpty())
lists.add(line.trim());
}
br.close();
if (lists.isEmpty())
System.out.println("No playlists found!");
} catch (Exception e) {
System.out.println(
"Playlist file missing: " +
e.getMessage());
}
return lists;
}
// Create new playlist file and register it
public String createPlaylist(String name) {
try {
String path = name + ".txt";
File f = new File("Playlists/" + path);
// If already exists
if (f.exists()) {
System.out.println("Playlist already exists!");
return null;
}
// create empty file
f.createNewFile();
// Add path to playlists.txt
FileWriter fw =
new FileWriter("playlists.txt", true);
fw.write(path + "\n");
fw.close();
return path;
} catch (Exception e) {
System.out.println(
"Error creating playlist: " +
e.getMessage());
return null;
}
}
// Add selected song path into playlist file
public void addSongToPlaylist( String playlistPath, String songPath) {
try {
FileWriter fw =
new FileWriter("Playlists/" + playlistPath, true);
fw.write(songPath + "\n");
fw.close();
System.out.println("Added: " + songPath);
} catch (Exception e) {
System.out.println(
"Cannot add song: " +
e.getMessage());
}
}
// Delete playlist: remove from playlists.txt AND delete file
public void deletePlaylist(String path) {
try {
// ===== 1. DELETE ACTUAL FILE =====
File f = new File("Playlists/" + path);
if (f.exists())
f.delete();
// ===== 2. REMOVE ENTRY FROM playlists.txt =====
File original = new File("playlists.txt");
File temp = new File("temp.txt");
BufferedReader br = new BufferedReader(new FileReader(original));
FileWriter fw = new FileWriter(temp);
String line;
while ((line = br.readLine()) != null) {
// write all except deleted one
if (!line.trim().equals(path))
fw.write(line + "\n");
}
br.close();
fw.close();
// replace old with new
original.delete();
temp.renameTo(original);
System.out.println("Playlist Deleted Successfully!");
} catch (Exception e) {
System.out.println(
"Delete Failed: " +
e.getMessage());
}
}
public void renamePlaylist(String oldPath, String newName) {
try {
String newPath = newName + ".txt";
File oldFile = new File("Playlists/" + oldPath);
File newFile = new File("Playlists/" + newPath);
// rename actual file
oldFile.renameTo(newFile);
// update playlists.txt
File original = new File("playlists.txt");
File temp = new File("temp.txt");
BufferedReader br = new BufferedReader(new FileReader(original));
FileWriter fw = new FileWriter(temp);
String line;
while ((line = br.readLine()) != null) {
if (line.trim().equals(oldPath))
fw.write(newPath + "\n");
else
fw.write(line + "\n");
}
br.close();
fw.close();
original.delete();
temp.renameTo(original);
System.out.println("Renamed Successfully!");
} catch (Exception e) {
System.out.println(
"Rename Failed: " + e.getMessage());
}
}
public void removeSong(String playlistPath, String songPath) {
try {
File original = new File("Playlists/" + playlistPath);
File temp = new File("temp.txt");
BufferedReader br = new BufferedReader(new FileReader(original));
FileWriter fw = new FileWriter(temp);
String line;
while ((line = br.readLine()) != null) {
if (!line.trim().equals(songPath))
fw.write(line + "\n");
}
br.close();
fw.close();
original.delete();
temp.renameTo(original);
System.out.println("Song Removed!");
} catch (Exception e) {
System.out.println(
"Remove Failed: " +
e.getMessage());
}
}
}