Arthur Brandao 6 vuotta sitten
vanhempi
sitoutus
8b2116c083
2 muutettua tiedostoa jossa 74 lisäystä ja 1 poistoa
  1. 1 1
      TDD2019OWNTESTS
  2. 73 0
      src/migl/util/FileGet.java

+ 1 - 1
TDD2019OWNTESTS

@@ -1 +1 @@
-Subproject commit 5f59d9bcfeb0d16422c0ea1e781b6672a465e876
+Subproject commit 70ec96437b3fe64b90eab0d40ef8134fe58578d5

+ 73 - 0
src/migl/util/FileGet.java

@@ -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();
+	}
+
+}