Java Code Examples for com.electronwill.nightconfig.core.file.FileConfig#close()

The following examples show how to use com.electronwill.nightconfig.core.file.FileConfig#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AutoreloadExample.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {
	// Creates an autoreloaded config:
	File configFile = new File("autoreload.json");
	FileConfig config = FileConfig.builder(configFile).autoreload().build();
	System.out.println("Config: " + config.valueMap());

	// Modifies the file:
	try (FileWriter writer = new FileWriter(configFile)) {
		writer.write("{ \"value\": 123 }");
	}
	Thread.sleep(1000);// The modifications take some (short) time to be detected
	System.out.println("Config: " + config);// You should see "Config: {value=123}"

	/* 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 autoreloaded config, the close() method tells the OS that it
	doesn't need to watch this file anymore. */
	config.close();
}
 
Example 2
Source File: JsonConfigTest.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Test
public void testAuto() throws InterruptedException {
	FileConfig config = FileConfig.builder(file).autoreload().autosave().build();
	config.load();
	System.out.println(config);
	double d;
	for (int i = 0; i < 20; i++) {
		d = Math.random();
		config.set("double", d);
		Thread.sleep(20);
		//System.out.println(i + ":" + d);
		assertEquals(d, config.<Double>get("double").doubleValue());
	}
	for (int i = 0; i < 1000; i++) {
		config.set("double", 0.123456);
	}
	config.close();
}
 
Example 3
Source File: AutosaveExample.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
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();
}
 
Example 4
Source File: JsonConfigTest.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testReadEmptyFile() throws IOException {
	File f = new File("tmp.json");
	FileConfig config = FileConfig.of(f);
	config.load();
	config.close();
	Config conf = new JsonParser().parse(f, FileNotFoundAction.THROW_ERROR);
	System.out.println(conf);
	assertTrue(conf.isEmpty());
	System.out.println("tmp.json:\n" + Files.readAllLines(f.toPath()).get(0));
	f.delete();
}
 
Example 5
Source File: FileConfigExample.java    From night-config with GNU Lesser General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
	// Creates the FileConfig:
	FileConfig config = FileConfig.of("config.toml");

	// Loads the config (reads the file):
	config.load();
	// Note: the load() call is always blocking: it returns when the reading operation terminates.
	// load() is also used to reload the configuration.

	System.out.println("Config: " + config);

	// Modifies the config:
	config.set("key", "value");
	config.set("number", 123456);
	config.set("floatingPoint", 3.1415926535);

	// You can also use sub configs!
	Config subConfig = Config.inMemory();
	subConfig.set("subKey", "value");
	config.set("subConfig", subConfig);

	// NightConfig supports dot-separated paths:
	config.set("subConfig.subKey", "newValue");

	// If you want to use a key that contains a dot in its name, use a list:
	config.set(Arrays.asList("subConfig", "127.0.0.1"),
			   "test");// the key "127.0.0.1" is in subConfig

	System.out.println("Config: " + config);

	// Saves the config:
	config.save();
	// Note: by default, the save operation is done in the background, and config.save()
	// returns immediately without waiting for the operation to terminates.

	/* Once you don't need the FileConfig anymore, remember to close it, in order to release
	the associated resources. There aren't always such resources, but it's a good practise to
	call the close() method anyway.
	Closing the FileConfig also ensures that all the data has been written, in particular it
	waits for the background saving operations to complete. */
	config.close();
}