- Replace `if (!x) ... else ...` with `if(x) ... else ...` - Avoid throwing RuntimeException; use children
59 lines
1.9 KiB
Java
59 lines
1.9 KiB
Java
package tools.utils;
|
|
|
|
import java.io.IOException;
|
|
import java.nio.charset.Charset;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.nio.file.StandardOpenOption;
|
|
import java.util.List;
|
|
|
|
/**
|
|
* Utility class for reading from and writing to files.
|
|
*/
|
|
public final class FileUtils {
|
|
|
|
private final static Charset CHARSET = Charset.forName("utf-8");
|
|
|
|
private FileUtils() {
|
|
}
|
|
|
|
public static void generateFileFromTemplate(String templateFile, String destinationFile, TagValueHolder tags) {
|
|
String template = readFromFile(templateFile);
|
|
String result = TagReplacer.applyReplacements(template, tags);
|
|
writeToFile(destinationFile, result);
|
|
}
|
|
|
|
public static void writeToFile(String outputFile, String contents) {
|
|
try {
|
|
Files.write(Paths.get(outputFile), contents.getBytes());
|
|
} catch (IOException e) {
|
|
throw new UnsupportedOperationException("Failed to write to file '" + outputFile + "'", e);
|
|
}
|
|
}
|
|
|
|
public static void appendToFile(String outputFile, String contents) {
|
|
try {
|
|
Files.write(Paths.get(outputFile), contents.getBytes(), StandardOpenOption.APPEND);
|
|
} catch (IOException e) {
|
|
throw new UnsupportedOperationException("Failed to append to file '" + outputFile + "'", e);
|
|
}
|
|
}
|
|
|
|
public static String readFromFile(String file) {
|
|
try {
|
|
return new String(Files.readAllBytes(Paths.get(file)), CHARSET);
|
|
} catch (IOException e) {
|
|
throw new UnsupportedOperationException("Could not read from file '" + file + "'", e);
|
|
}
|
|
}
|
|
|
|
public static List<String> readLinesFromFile(String file) {
|
|
try {
|
|
return Files.readAllLines(Paths.get(file), CHARSET);
|
|
} catch (IOException e) {
|
|
throw new UnsupportedOperationException("Could not read from file '" + file + "'", e);
|
|
}
|
|
}
|
|
|
|
}
|