-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTools.java
49 lines (43 loc) · 1.71 KB
/
Tools.java
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
import java.io.File;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import com.google.gson.JsonElement;
public class Tools {
public static String getHttpData(String httpURL){
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(httpURL)).build();
HttpResponse<String> response;
try {
response = client
.send(request,HttpResponse.BodyHandlers.ofString());
} catch (Exception e) { throw new RuntimeException(e); }
return response != null ? response.body() : null;
}
public static void saveToFile(JsonElement jEle,String filePath){
try {
createFile(filePath);
writeFile(filePath,jEle.toString());
} catch (Exception e) { throw new RuntimeException(e); }
}
public static void writeFile(String filePath,String content){
FileOutputStream fos ;
try {
fos = new FileOutputStream(filePath, true);
fos.write(content.getBytes());
fos.flush();
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
}
public static void createFile(String newFilePath){
File file = new File(newFilePath);
File parentDir = file.getParentFile();
try {
if (!parentDir.exists()) if(!parentDir.mkdirs()) throw new RuntimeException("Make new directory failed");
if(!file.exists()) file.createNewFile(); else throw new RuntimeException("File is exist");
}catch (Exception e){ throw new RuntimeException(e); }
}
}