-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathAutosaveExample.java
More file actions
52 lines (44 loc) · 1.62 KB
/
AutosaveExample.java
File metadata and controls
52 lines (44 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import com.electronwill.nightconfig.core.file.FileConfig;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
/**
* @author TheElectronWill
*/
public class AutosaveExample {
public static void main(String[] args) throws IOException, InterruptedException {
// Creates an autosaved config:
File configFile = new File("autosave.json");
FileConfig config = FileConfig.builder(configFile).autosave().build();
// Reads the file:
printFile(configFile);
// Modifies the config:
config.set("value", 123);
Thread.sleep(500);
/* The config we built is in "asynchronous writing" mode, which means that the writing
operations occurs in the background, without blocking the config.set method. That's why
to see the modifications in our program we have to wait a little bit.
Try adding sync() to the build chain and removing the Thread.sleep instruction!
*/
printFile(configFile);
// Modifies the config again:
config.set("value", 1234);
Thread.sleep(500);
printFile(configFile);
/* Don't forget to close the config! In that case the program terminates so it's not a
big deal, but otherwise it's important to close the FileConfig in order to release the
associated resources. For an autosaved config, the close() method discards the
channel/stream used to save the config. */
config.close();
}
private static void printFile(File file) throws IOException {
System.out.println("--- config file ---");
if (file.exists()) {
for (String line : Files.readAllLines(file.toPath())) {
System.out.println(line);
}
} else {
System.out.println("The file doesn't exist.");
}
}
}