aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/FileUtility.java
blob: 936cecddfac20eabc1be4a72eaf9d481797e9c40 (plain) (blame)
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
import org.json.JSONObject;

import java.io.*;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Objects;


public class FileUtility {

    public static void deleteFile(String fileName) {
        File file = new File(fileName);
        if (file.exists()) {
            if(!file.delete())
                System.out.println("Failed to delete file: " + fileName);
        }
    }

    public static void deleteALlFileOfType(String path, String fileExt){
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        for(int i = 0; i< Objects.requireNonNull(listOfFiles).length; i++){
            if(listOfFiles[i].isFile()){
                if(listOfFiles[i].getName().endsWith(fileExt)){
                    listOfFiles[i].delete();
                }
            }
        }
    }

    public static String downloadImage(String url, String fileName, String[] formats)  {
        boolean successfulDownload = false;
        int formatIndex = 0;
        String pathToImage = "";
        while(!successfulDownload) {
            System.out.println("Attempting to download image at: " + url+formats[formatIndex]);
            // attempt to download image
            for (int i = 0; i < 3; i++) {
                try {
                    URL imageUrl = new URL(url + formats[formatIndex]);
                    InputStream in = imageUrl.openStream();
                    Path path = Paths.get(fileName);
                    OutputStream out = new BufferedOutputStream(Files.newOutputStream(path));
                    for (int b; (b = in.read()) != -1; ) {
                        out.write(b);
                    }
                    out.close();
                    in.close();
                    successfulDownload = true;
                    pathToImage = path.toAbsolutePath().toString();
                    break;
                } catch (Exception e) {
                    System.out.println("Failed to download image at: " + url+formats[formatIndex]);
                    System.out.println("Retrying...");
                }
            }

            if(formatIndex > formats.length - 1){
                break;
            }
            if(!successfulDownload){
                formatIndex++;
            }
        }
        System.out.println("Image downloaded");
        return pathToImage;
    }

    public static File findFileWithType(String directory, String fileExt){
        System.out.println("Searching for file with extension: " + fileExt + " in directory: " + directory);
        File dir = new File(directory);
        File[] files = dir.listFiles();
        assert files != null;
        for(File file : files){
            if(file.getName().endsWith(fileExt)){
                System.out.println("Found file: " + file.getName());
                return file;
            }
        }
        System.out.println("No file found with extension: " + fileExt);
        return null;
    }

    public static boolean findFileContainingString(String directory, String searchString){
        File dir = new File(directory);
        File[] files = dir.listFiles();
        assert files != null;
        for(File file : files){
            if(file.getName().contains(searchString)){
                return true;
            }
        }
        return false;
    }


    public static String findJsonFile(String folderName) {
        File folder = new File(folderName);
        File[] listOfFiles = folder.listFiles();
        for (int i = 0; i < Objects.requireNonNull(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 ignored) {
        }
        return json;
    }



    public static ArrayList<String> txtToList(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) {
            System.out.println("Error reading file: " + fileName);
        }
        return lines;
    }

    //get the path of all mp3 files in a directory and return them as a file arraylist
    public static ArrayList<File> getMp3FilesAsList(String path){
        ArrayList<File> mp3Files = new ArrayList<File>();
        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();

        for(int i = 0; i< Objects.requireNonNull(listOfFiles).length; i++){
            if(listOfFiles[i].isFile()){
                if(listOfFiles[i].getName().endsWith(".mp3")){
                    mp3Files.add(listOfFiles[i]);
                }
            }
        }
        return mp3Files;
    }

}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage