Sauvegarde et lecture du fichier de synchronisation -> Méthodes save et load terminée

This commit is contained in:
Maximilien LEDOUX 2022-03-08 09:32:26 +01:00
parent d4cff6de09
commit 26be3ca8ff
3 changed files with 43 additions and 13 deletions

View File

@ -2,15 +2,8 @@ package lightcontainer.storage;
public interface Adapter {
/**
* @param appData The appData to stringify
* @return The string representation of AppData
*/
String toString(AppData appData);
/**
* @param appDataString The appData string to parse
* @return AppData
*/
String toString();
AppData fromString(String appDataString);
}

View File

@ -6,8 +6,14 @@ import java.util.*;
public class JsonAdapter implements Adapter {
private AppData appData;
public JsonAdapter(AppData appData) {
this.appData = appData;
}
@Override
public String toString(AppData appData) {
public String toString() {
return addData(appData);
}
@ -81,7 +87,8 @@ public class JsonAdapter implements Adapter {
for (User user : users) {
appData.addUser(user);
}
return appData;
this.appData = appData;
return this.appData;
} catch (JsonParseException parseException) {
System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log
}

View File

@ -1,14 +1,44 @@
package lightcontainer.storage;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
public class Repository {
//static final String CONFIG_FILE_PATH = "../../resources/config.json";
void save(String filePath, Adapter adapter) {
if (filePath != null) {
String jsonAppData = adapter.toString();
try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(filePath), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
bufferedWriter.write(jsonAppData);
bufferedWriter.flush();
} catch (IOException e) {
System.out.println("Error while saving configuration file !");
}
}
}
AppData load(String filePath, Adapter adapter) {
return null;
String jsonString = readFile(filePath);
return adapter.fromString(jsonString);
}
private String readFile(String filePath) {
StringBuilder builder = new StringBuilder();
try (BufferedReader reader = Files.newBufferedReader(Paths.get(filePath), StandardCharsets.UTF_8)) {
while (reader.ready()) {
builder.append(reader.readLine());
}
} catch (IOException e) {
System.out.println("Error while reading configuration file");
builder.setLength(0);
}
return builder.toString();
}
}