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
|
import org.json.JSONArray;
import org.json.JSONObject;
import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class FileUtility {
public void deleteFile(String fileName) {
File file = new File(fileName);
if (file.exists()) {
file.delete();
}
}
public void downloadImage(String url, String fileName,String[] formats) {
boolean successfulDownload = false;
int formatIndex = 0;
while(!successfulDownload) {
try {
FileOutputStream fos = new FileOutputStream(fileName);
URL urlObj = new URL(url+formats[formatIndex]);
InputStream is = urlObj.openStream();
byte[] b = new byte[2048];
int length;
while ((length = is.read(b)) != -1) {
fos.write(b, 0, length);
}
fos.close();
is.close();
successfulDownload = true;
} catch (Exception e) {
formatIndex++;
}
}
}
public String removeBlacklist(String s, String filename){
HashMap<String, String> blacklist = arrayListToHashMap(readTextFile(filename),":");
for(String key : blacklist.keySet()){
if(s.contains(key)){
s = s.replace(key,blacklist.get(key));
}
}
return s;
}
//read a text file and return the contents as a hashmap with key value pairs
public ArrayList<String> readTextFile(String fileName) {
ArrayList<String> lines = new ArrayList<String>();
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return lines;
}
public HashMap<String, String> arrayListToHashMap(ArrayList<String> list, String delimiter) {
HashMap<String, String> map = new HashMap<String, String>();
for (String line : list) {
String[] parts = line.split(delimiter);
if (parts.length >= 2) {
String key = parts[0];
String value = parts[1];
map.put(key, value);
}
else if(parts.length==1){
String key = parts[0];
String value = "";
map.put(key, value);
}
else {
System.out.println("ignoring line: " + line);
}
}
return map;
}
public void moveFile(String source, String destination) {
File sourceFile = new File(source);
File destinationFile = new File(destination);
sourceFile.renameTo(destinationFile);
System.out.println("Moved file to Completed Folder");
}
public String removeNonAlphaNumeric(String str) {
return str.replaceAll("[^a-zA-Z0-9]", "");
}
public static String showTextFileChooser() {
javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt", "text");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Select a text file");
chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile().getAbsolutePath();
} else {
return null;
}
}
public static File findFileType(String directory, String fileExt){
File dir = new File(directory);
File[] files = dir.listFiles();
for(File file : files){
if(file.getName().endsWith(fileExt)){
return file;
}
}
return null;
}
public static String findJsonFile(String folderName) {
File folder = new File(folderName);
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if (listOfFiles[i].getName().endsWith(".json")) {
return listOfFiles[i].getAbsolutePath();
}
}
}
return null;
}
public static String[] parseInfoJSON(String json) {
JSONObject obj = new JSONObject(json);
String title = obj.getString("fulltitle");
String uploader = obj.getString("uploader");
String id = obj.getString("id");
String[] info = {title,uploader,id};
return info;
}
public static String jsonToString(String fileName) {
String json = "";
try {
BufferedReader br = new BufferedReader(new FileReader(fileName));
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
sb.append(line);
line = br.readLine();
}
json = sb.toString();
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
public static void deleteAllFilesDir(String path) {
File folder = new File(path);
File[] files = folder.listFiles();
if (files != null) {
for (File f : files) {
f.delete();
}
}
}
public static String showImageFileChooser() {
javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("JPEG Image File", "jpg", "jpeg");
chooser.setFileFilter(filter);
chooser.setDialogTitle("Select a image file");
chooser.setFileSelectionMode(javax.swing.JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == javax.swing.JFileChooser.APPROVE_OPTION) {
return chooser.getSelectedFile().getAbsolutePath();
} else {
return null;
}
}
public static ArrayList<String> txtToArrayList(String fileName) {
ArrayList<String> lines = new ArrayList<String>();
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
}
return lines;
}
public String showDirectoryChooser(){
try {
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int result = fileChooser.showOpenDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
return fileChooser.getSelectedFile().getAbsolutePath();
}
}
catch(Exception e){
JOptionPane.showMessageDialog(null,"An unexpected error has occured");
}
return "";
}
//get the path of all mp3 files in a directory and return them as a file arraylist
public ArrayList<File> getMp3Files(String path){
ArrayList<File> mp3Files = new ArrayList<File>();
File folder = new File(path);
File[] listOfFiles = folder.listFiles();
for(int i=0;i<listOfFiles.length;i++){
if(listOfFiles[i].isFile()){
if(listOfFiles[i].getName().endsWith(".mp3")){
mp3Files.add(listOfFiles[i]);
}
}
}
return mp3Files;
}
}
|