Update Files.java

This commit is contained in:
坏黑 2019-07-09 21:53:38 +08:00
parent 59a3b609ca
commit 1cabe6c14c

View File

@ -224,6 +224,10 @@ public class Files {
return Optional.ofNullable(readFromURL(url)).orElse(def); return Optional.ofNullable(readFromURL(url)).orElse(def);
} }
public static String readFromURL(String url, Charset charset, String def) {
return Optional.ofNullable(readFromURL(url, charset)).orElse(def);
}
public static String readFromURL(String url) { public static String readFromURL(String url) {
try (InputStream inputStream = new URL(url).openStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) { try (InputStream inputStream = new URL(url).openStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
return new String(IO.readFully(bufferedInputStream)); return new String(IO.readFully(bufferedInputStream));
@ -233,6 +237,15 @@ public class Files {
return null; return null;
} }
public static String readFromURL(String url, Charset charset) {
try (InputStream inputStream = new URL(url).openStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream)) {
return new String(IO.readFully(bufferedInputStream), charset);
} catch (Throwable t) {
t.printStackTrace();
}
return null;
}
public static String readFromFile(File file) { public static String readFromFile(File file) {
return readFromFile(file, 1024, StandardCharsets.UTF_8); return readFromFile(file, 1024, StandardCharsets.UTF_8);
} }