<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-properties</artifactId>
<version>2.12.5</version>
</dependency>
import com.fasterxml.jackson.dataformat.properties.PropertiesMapper;
import java.io.File;
public class PropertiesReader {
public static void main(String[] args) throws Exception {
PropertiesMapper mapper = new PropertiesMapper();
MyProperties properties = mapper.readValue(new File("example.properties"), MyProperties.class);
}
}
class MyProperties {
private String key1;
private String key2;
// ...
}
import com.fasterxml.jackson.dataformat.properties.PropertiesMapper;
import java.io.File;
public class PropertiesWriter {
public static void main(String[] args) throws Exception {
PropertiesMapper mapper = new PropertiesMapper();
MyProperties properties = new MyProperties();
properties.setKey1("value1");
properties.setKey2("value2");
mapper.writeValue(new File("example.properties"), properties);
}
}
class MyProperties {
private String key1;
private String key2;
// ...
}
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import java.util.Map;
public class PropertiesReader {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Map<String, String> properties = mapper.readValue(new File("example.properties"), new TypeReference<Map<String, String>>(){});
}
}
public class PropertiesWriter {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
Map<String, String> properties = new HashMap<>();
properties.put("key1", "value1");
properties.put("key2", "value2");
mapper.writeValue(new File("example.properties"), properties);
}
}
java -XX:+AggressiveOpts YourMainClass
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonGenerator;
import java.util.Properties;
import java.io.File;
public class PropertiesReader {
public static void main(String[] args) throws Exception {
JsonFactory factory = new JsonFactory();
JsonParser parser = factory.createParser(new File("example.properties"));
Properties properties = new Properties();
while (parser.nextToken() != null) {
String key = parser.getCurrentName();
parser.nextToken();
String value = parser.getValueAsString();
properties.setProperty(key, value);
}
}
}
public class PropertiesWriter {
public static void main(String[] args) throws Exception {
JsonFactory factory = new JsonFactory();
JsonGenerator generator = factory.createGenerator(new File("example.properties"));
generator.writeStartObject();
generator.writeStringField("key1", "value1");
generator.writeStringField("key2", "value2");
generator.writeEndObject();
generator.close();
}
}