|
@@ -0,0 +1,73 @@
|
|
|
+package migl.util;
|
|
|
+
|
|
|
+import java.io.File;
|
|
|
+import java.io.IOException;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.nio.file.Files;
|
|
|
+import java.nio.file.Paths;
|
|
|
+import java.util.stream.Stream;
|
|
|
+
|
|
|
+public class FileGet {
|
|
|
+
|
|
|
+ private File file;
|
|
|
+ private String before;
|
|
|
+
|
|
|
+ public FileGet(String path) {
|
|
|
+ this(path, "");
|
|
|
+ }
|
|
|
+
|
|
|
+ public FileGet(String path, String before) {
|
|
|
+ this.file = new File(path);
|
|
|
+ this.before = before;
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean exists() {
|
|
|
+ return this.file.exists();
|
|
|
+ }
|
|
|
+
|
|
|
+ public boolean findFile(String fileName) {
|
|
|
+ if(!this.exists()) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ File[] files = this.file.listFiles();
|
|
|
+ for(File f : files) {
|
|
|
+ if(f.getName().equals(fileName)) {
|
|
|
+ this.file = f;
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getFiles() {
|
|
|
+ if(!this.exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(!this.file.isDirectory()) {
|
|
|
+ return this.getContent();
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder(this.before);
|
|
|
+ File[] files = this.file.listFiles();
|
|
|
+ for(File f : files) {
|
|
|
+ sb.append(f.getName() + "\n");
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getContent() {
|
|
|
+ if(!this.exists()) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ if(!this.file.isFile()) {
|
|
|
+ return this.getFiles();
|
|
|
+ }
|
|
|
+ StringBuilder sb = new StringBuilder(this.before);
|
|
|
+ try(Stream<String> stream = Files.lines(Paths.get(this.file.getAbsolutePath()), StandardCharsets.UTF_8)){
|
|
|
+ stream.forEach(s -> sb.append(s + "\n"));
|
|
|
+ } catch(IOException ex) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|