Skip to content

Commit

Permalink
feat(utils): writeStringToFile method using UTF-8 charset
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Mar 31, 2022
1 parent 45dbf1d commit 97bb827
Showing 1 changed file with 17 additions and 9 deletions.
26 changes: 17 additions & 9 deletions src/main/java/org/auioc/mods/arnicalib/utils/java/FileUtils.java
Expand Up @@ -5,6 +5,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
import org.apache.logging.log4j.Marker;
import org.auioc.mods.arnicalib.utils.LogUtil;

Expand All @@ -15,9 +16,7 @@ public class FileUtils extends org.apache.commons.io.FileUtils {
public static File getOrCreateDirectory(String directoryName) throws IOException {
var file = new File(directoryName);

if (file.exists()) {
return file;
}
if (file.exists()) return file;

if (file.mkdirs()) {
LOGGER.warn(MARKER, "Directory \"" + file + "\" does not exist, create");
Expand All @@ -29,9 +28,7 @@ public static File getOrCreateDirectory(String directoryName) throws IOException
public static File getFile(String fileName) throws IOException {
var file = new File(fileName);

if (file.getParentFile().exists()) {
return file;
}
if (file.getParentFile().exists()) return file;

if (file.getParentFile().mkdirs()) {
LOGGER.warn(MARKER, "Parent directory of file \"" + file + "\" does not exist, create");
Expand All @@ -40,6 +37,19 @@ public static File getFile(String fileName) throws IOException {
throw new IOException("Could not create parent directory of file \"" + file + "\"");
}


/**
* Writes a String to a file creating the file if it does not exist using the UTF-8 charset.
*
* @param file the file to write
* @param data the content to write to the file
* @throws IOException in case of an I/O error
* @since 5.1.4
*/
public static void writeStringToFile(final File file, final String data) throws IOException {
writeStringToFile(file, data, Charset.forName("utf8"), false);
}

/* ============================================================================================================== */
// #region Deprecated

Expand All @@ -50,9 +60,7 @@ private FileUtils() {}
public static void writeText(String fileName, String text) throws IOException {
var file = getFile(fileName);

if (file.exists()) {
LOGGER.warn(MARKER, "File \"" + file + "\" already exists, overwrite");
}
if (file.exists()) LOGGER.warn(MARKER, "File \"" + file + "\" already exists, overwrite");

final var writer = new BufferedWriter(new FileWriter(file, false));
writer.write(text);
Expand Down

0 comments on commit 97bb827

Please sign in to comment.