Sauvegarde et lecture du fichier de synchronisation -> Méthodes save et load terminée
This commit is contained in:
parent
d4cff6de09
commit
26be3ca8ff
@ -2,15 +2,8 @@ package lightcontainer.storage;
|
|||||||
|
|
||||||
public interface Adapter {
|
public interface Adapter {
|
||||||
|
|
||||||
/**
|
|
||||||
* @param appData The appData to stringify
|
|
||||||
* @return The string representation of AppData
|
|
||||||
*/
|
|
||||||
String toString(AppData appData);
|
|
||||||
|
|
||||||
/**
|
String toString();
|
||||||
* @param appDataString The appData string to parse
|
|
||||||
* @return AppData
|
|
||||||
*/
|
|
||||||
AppData fromString(String appDataString);
|
AppData fromString(String appDataString);
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,14 @@ import java.util.*;
|
|||||||
|
|
||||||
public class JsonAdapter implements Adapter {
|
public class JsonAdapter implements Adapter {
|
||||||
|
|
||||||
|
private AppData appData;
|
||||||
|
|
||||||
|
public JsonAdapter(AppData appData) {
|
||||||
|
this.appData = appData;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString(AppData appData) {
|
public String toString() {
|
||||||
return addData(appData);
|
return addData(appData);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -81,7 +87,8 @@ public class JsonAdapter implements Adapter {
|
|||||||
for (User user : users) {
|
for (User user : users) {
|
||||||
appData.addUser(user);
|
appData.addUser(user);
|
||||||
}
|
}
|
||||||
return appData;
|
this.appData = appData;
|
||||||
|
return this.appData;
|
||||||
} catch (JsonParseException parseException) {
|
} catch (JsonParseException parseException) {
|
||||||
System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log
|
System.out.println("[FFE] : Error while loading configuration file"); //TODO - changer en log
|
||||||
}
|
}
|
||||||
|
@ -1,14 +1,44 @@
|
|||||||
package lightcontainer.storage;
|
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 {
|
public class Repository {
|
||||||
|
|
||||||
//static final String CONFIG_FILE_PATH = "../../resources/config.json";
|
//static final String CONFIG_FILE_PATH = "../../resources/config.json";
|
||||||
|
|
||||||
void save(String filePath, Adapter adapter) {
|
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) {
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user