Java Code Examples for com.electronwill.nightconfig.core.CommentedConfig#set()

The following examples show how to use com.electronwill.nightconfig.core.CommentedConfig#set() . 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: CommentedConfigExample.java    From night-config with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void main(String[] args) {
	// Creates a CommentedConfig in memory (ie not linked to a file)
	CommentedConfig config = CommentedConfig.inMemory();
	config.set("key", "value");
	config.setComment("key", "This is a comment.");

	String comment = config.getComment("key");
	System.out.println("Comment of the key: " + comment);
	System.out.println("Config: " + config);

	/* An non-FileConfig cannot be saved with a save() method, but any config can be saved by
	 an appropriate ConfigWriter. Here we'll use a TomlWriter to write the config in the TOML
	 format. */
	File configFile = new File("commentedConfig.toml");
	TomlWriter writer = new TomlWriter();
	writer.write(config, configFile, WritingMode.REPLACE);
	/* That's it! The config has been written to the file. Note that there is no need to close
	 the writer nor to catch any exception. */
}
 
Example 2
Source File: HoconWriterTest.java    From night-config with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Test
public void testWrite() throws IOException {
	Config subConfig = CommentedConfig.inMemory();
	subConfig.set("string", "test");
	subConfig.set("enum", BasicTestEnum.C);
	subConfig.set("sub", CommentedConfig.inMemory());

	List<Config> configList = new ArrayList<>();
	configList.add(subConfig);
	configList.add(subConfig);
	configList.add(subConfig);

	CommentedConfig config = CommentedConfig.inMemory();
	config.set("string", "\"value\"");
	config.set("integer", 2);
	config.set("long", 123456789L);
	config.set("double", 3.1415926535);
	config.set("bool_array", Arrays.asList(true, false, true, false));
	config.set("config", subConfig);
	config.set("config_list", configList);
	config.setComment("string", " Comment 1\n Comment 2\n Comment 3");
	config.set("enum", TestEnum.A);

	StringWriter sw = new StringWriter();
	HoconWriter writer = new HoconWriter();
	writer.write(config, sw);
	System.out.println("Written:");
	System.out.println(sw);
}