improved file writing by outsourcing String reading and including "Reader"s
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
package FIXME;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.MathContext;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.text.DecimalFormat;
|
||||
import java.time.Duration;
|
||||
@@ -15,6 +15,7 @@ import java.util.concurrent.TimeUnit;
|
||||
public class FabianUtil {
|
||||
|
||||
private static final String STD_DIR_TEMP = "/FIXME/temp/";
|
||||
private static final String TEST_OUTPUT_DIR = "testOutput/";
|
||||
public static final String ENDING_TXT = "txt";
|
||||
public static final String ENDING_JSON = "json";
|
||||
public static final String ENDING_HTML = "html";
|
||||
@@ -224,22 +225,27 @@ public class FabianUtil {
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, String content) {
|
||||
writeTestOutput(filename, ending, strToBytes(content));
|
||||
}
|
||||
|
||||
|
||||
public static byte[] strToBytes(String str) {
|
||||
return str.getBytes(StandardCharsets.UTF_8);
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, byte[] content) {
|
||||
writeToFile(STD_DIR_TEMP + "testOutput/", filename, ending, content);
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, InputStream content) {
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeTestOutput(String filename, String ending, Reader content) {
|
||||
writeToFile(STD_DIR_TEMP + TEST_OUTPUT_DIR, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String ending, String content) {
|
||||
writeDump(ending, strToBytes(content));
|
||||
writeToFile(STD_DIR_TEMP, "dump", ending, content);
|
||||
}
|
||||
|
||||
|
||||
@@ -248,8 +254,18 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String ending, InputStream content) {
|
||||
writeToFile(STD_DIR_TEMP, "dump", ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String ending, Reader content) {
|
||||
writeToFile(STD_DIR_TEMP, "dump", ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String filename, String ending, String content) {
|
||||
writeDump(filename, ending, strToBytes(content));
|
||||
writeToFile(STD_DIR_TEMP, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
@@ -258,9 +274,45 @@ public class FabianUtil {
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String filename, String ending, InputStream content) {
|
||||
writeToFile(STD_DIR_TEMP, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeDump(String filename, String ending, Reader content) {
|
||||
writeToFile(STD_DIR_TEMP, filename, ending, content);
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, String content) {
|
||||
writeToFile(path, filename, ending, new StringReader(content));
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, byte[] content) {
|
||||
try {
|
||||
writeToFile(path, filename, ending, new InputStreamReader(new ByteArrayInputStream(content)));
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, InputStream content) {
|
||||
writeToFile(path, filename, ending, new InputStreamReader(content));
|
||||
}
|
||||
|
||||
|
||||
public static void writeToFile(String path, String filename, String ending, Reader content) {
|
||||
Path toFile = Paths.get(path + filename + "." + ending);
|
||||
try (content) {
|
||||
BufferedWriter out = Files.newBufferedWriter(toFile);
|
||||
content.transferTo(out);
|
||||
/* varianten: selber pipen:
|
||||
OutputStream out = Files.newOutputStream(Paths.get(path + filename + "." + ending));
|
||||
content.transferTo(out);
|
||||
oder mit reader/ writer:
|
||||
BufferedWriter out = Files.newBufferedWriter(Paths.get(path + filename + "." + ending));
|
||||
content.transferTo(out);
|
||||
oder mit byte-array:
|
||||
Files.write(Paths.get(path + filename + "." + ending), content);
|
||||
*/
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
Reference in New Issue
Block a user