From 43d01ac420546167a33dcd5c66881d4205d8f551 Mon Sep 17 00:00:00 2001 From: RuscalWorld Date: Mon, 24 May 2021 20:57:29 +0300 Subject: [PATCH] Fix saving of default config --- .../fabricexporter/config/Config.java | 10 +++---- .../fabricexporter/util/FileUtil.java | 28 +++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ru/ruscalworld/fabricexporter/util/FileUtil.java diff --git a/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java b/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java index d3fb371..6865da3 100644 --- a/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java +++ b/src/main/java/ru/ruscalworld/fabricexporter/config/Config.java @@ -3,6 +3,7 @@ package ru.ruscalworld.fabricexporter.config; import net.fabricmc.loader.api.FabricLoader; import org.apache.logging.log4j.LogManager; import ru.ruscalworld.fabricexporter.FabricExporter; +import ru.ruscalworld.fabricexporter.util.FileUtil; import java.io.File; import java.io.IOException; @@ -10,6 +11,7 @@ import java.io.InputStream; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.List; import java.util.Properties; public abstract class Config { @@ -22,13 +24,11 @@ public abstract class Config { public void load() throws IOException { Path path = FabricLoader.getInstance().getConfigDir().resolve(this.getFileName()); if (!path.toFile().exists()) try { - URL url = this.getClass().getClassLoader().getResource("config/" + this.getFileName()); - assert url != null; - File file = new File(url.toURI()); - byte[] bytes = Files.readAllBytes(file.toPath()); + InputStream stream = this.getClass().getClassLoader().getResourceAsStream("config/" + this.getFileName()); + List lines = FileUtil.getLinesFromStream(stream); Files.createFile(path); - Files.write(path, bytes); + Files.write(path, lines); } catch (Exception exception) { exception.printStackTrace(); FabricExporter.getLogger().fatal("Unable to save default config"); diff --git a/src/main/java/ru/ruscalworld/fabricexporter/util/FileUtil.java b/src/main/java/ru/ruscalworld/fabricexporter/util/FileUtil.java new file mode 100644 index 0000000..27f8c8a --- /dev/null +++ b/src/main/java/ru/ruscalworld/fabricexporter/util/FileUtil.java @@ -0,0 +1,28 @@ +package ru.ruscalworld.fabricexporter.util; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.List; + +public class FileUtil { + /** + * Reads stream into a list of lines + * @param stream Input stream + * @return List of lines + */ + public static List getLinesFromStream(InputStream stream) throws IOException { + List result = new ArrayList<>(); + + InputStreamReader streamReader = new InputStreamReader(stream, StandardCharsets.UTF_8); + BufferedReader reader = new BufferedReader(streamReader); + + String line; + while ((line = reader.readLine()) != null) result.add(line); + + return result; + } +}