<dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-properties</artifactId> <version>2.12.5</version> </dependency> properties db.url=jdbc:mysql://localhost:3306/mydb db.username=root db.password=123456 public class DatabaseConfig { private String url; private String username; private String password; } public class Main { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(new PropertiesFactory()); DatabaseConfig config = objectMapper.readValue(new File("config.properties"), DatabaseConfig.class); System.out.println("URL: " + config.getUrl()); System.out.println("Username: " + config.getUsername()); System.out.println("Password: " + config.getPassword()); } } public class Main { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(new PropertiesFactory()); DatabaseConfig config = new DatabaseConfig(); config.setUrl("jdbc:mysql://localhost:3306/mydb"); config.setUsername("admin"); config.setPassword("password"); objectMapper.writeValue(new File("config.properties"), config); } } PropertiesFactory factory = new PropertiesFactory(Charset.forName("GBK")); ObjectMapper objectMapper = new ObjectMapper(factory);


上一篇:
下一篇:
切换中文