0

Made file saving run async

This commit is contained in:
Pepich 2017-02-01 17:11:16 +01:00
parent c89b43d247
commit 0e46725dab

View File

@ -15,7 +15,7 @@ import com.redstoner.annotations.Version;
/** This class provides simple JSON handling, like storing and loading from and to files. /** This class provides simple JSON handling, like storing and loading from and to files.
* *
* @author Pepich */ * @author Pepich */
@Version(major = 1, minor = 0, revision = 0, compatible = -1) @Version(major = 1, minor = 0, revision = 1, compatible = -1)
public class JsonManager public class JsonManager
{ {
private JsonManager() private JsonManager()
@ -43,26 +43,35 @@ public class JsonManager
return null; return null;
} }
/** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself. /** Saves a JSONObject to a file. Will create the necessary FileStructure like folders and the file itself.</br>
* Note that this operation will be run on a different thread and you do not need to take care of that yourself.
* *
* @param object the JSONObject to save. * @param object the JSONObject to save.
* @param destination the file to write to. */ * @param destination the file to write to. */
public static void save(JSONObject object, File destination) public static void save(JSONObject object, File destination)
{ {
if (destination.exists()) Thread t = new Thread(new Runnable()
destination.delete();
else if (!destination.getParentFile().exists())
destination.getParentFile().mkdirs();
try
{ {
destination.createNewFile(); @Override
FileWriter writer = new FileWriter(destination); public void run()
object.writeJSONString(writer); {
writer.flush(); if (destination.exists())
writer.close(); destination.delete();
} else if (!destination.getParentFile().exists())
catch (IOException e) destination.getParentFile().mkdirs();
{} try
{
destination.createNewFile();
FileWriter writer = new FileWriter(destination);
object.writeJSONString(writer);
writer.flush();
writer.close();
}
catch (IOException e)
{}
}
});
t.start();
} }
/** Loads a JSONArray from a file. /** Loads a JSONArray from a file.
@ -85,25 +94,34 @@ public class JsonManager
return null; return null;
} }
/** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself. /** Saves a JSONArray to a file. Will create the necessary FileStructure like folders and the file itself.</br>
* Note that this operation will be run on a different thread and you do not need to take care of that yourself.
* *
* @param object the JSONArray to save. * @param object the JSONArray to save.
* @param destination the file to write to. */ * @param destination the file to write to. */
public static void save(JSONArray array, File destination) public static void save(JSONArray array, File destination)
{ {
if (destination.exists()) Thread t = new Thread(new Runnable()
destination.delete();
else if (!destination.getParentFile().exists())
destination.getParentFile().mkdirs();
try
{ {
destination.createNewFile(); @Override
FileWriter writer = new FileWriter(destination); public void run()
array.writeJSONString(writer); {
writer.flush(); if (destination.exists())
writer.close(); destination.delete();
} else if (!destination.getParentFile().exists())
catch (IOException e) destination.getParentFile().mkdirs();
{} try
{
destination.createNewFile();
FileWriter writer = new FileWriter(destination);
array.writeJSONString(writer);
writer.flush();
writer.close();
}
catch (IOException e)
{}
}
});
t.start();
} }
} }