Assuming your .txt is some definitions, as long as the object that the definitions data are loaded into is Serializable (usually is), you can simply use Gson utilities to perform this easily.
For example, if this is how your definition object looks like:
Code:
class ItemDefinition {
final String name;
final int id;
...
//construction isn't necessary, since it deserialize by the fields (thus transient will not be supported, unless indicated otherwise)
ItemDefinition(String name, int id) {
this.name = name;
this.id = id;
}
}
Say this is your current definitions, this is the methods you'll need to workout with:
serialize
Code:
/*private static final*/Path PATH = Paths.get("data", "def", "items");
File fileToSave = new File(PATH.normalize().toFile().getAbsoluteFile() + File.separator + fileName + ".json");
if (!fileToSave.exists()) {
try {
if (!fileToSave.createNewFile()) {
throw new IllegalStateException("Couldn't create file for item" + fileToSave.getName());
}
} catch (IOException e) {
e.printStackTrace();
}
}
try (FileWriter fw = new FileWriter(fileToSave)) {
fw.write(GSON.toJson(STORE_HERE_WHATEVER_OBJECT)); //i.e ItemDefinitions.get(id)
System.out.println("written to file " + mobDrop);
} catch (Exception e) {
e.printStackTrace();
}
deserialize
Code:
/*private static final*/Path PATH = Paths.get("data", "def", "items");
File[] files = PATH.toFile().listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f == null || f.isDirectory()) continue;
try (FileReader reader = new FileReader(f)) {
ItemDefinition item= GSON.fromJson(reader, new TypeToken<ItemDefinition >() {
}.getType());
//item is deserialized (loaded), you can now use it
ItemDefinitions.put( item);
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
throw new IllegalStateException("No Items loaded.");
}
If you'd like further and more proper explaination I'l'l gladly assist if you'd pm me your discord (or quote it)