gpt4 book ai didi

java - 从文件加载和保存属性

转载 作者:行者123 更新时间:2023-11-29 05:23:45 26 4
gpt4 key购买 nike

在我的 spring 项目中,我的类路径中有一个名为 database.properties 的文件,其内容如下:

jdbc.Classname=org.postgresql.Driver
jdbc.url=
jdbc.user=
jdbc.pass=
hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
hibernate.show_sql=false
hibernate.hbm2ddl.auto=validate

我在我的一个服务类中有一个方法,我通过 hibernate 手动将数据库模式导出到服务器。这个方法此时的代码是这样的:

public void create_tables(String maquina, String usuario, String senha) {
Configuration config = new Configuration();
SchemaExport schema = new SchemaExport(config);
schema.create(true, true);
}

我想从文件 database.properties 加载属性到我的配置变量中,设置我传递给这个变量中的方法的值(url、用户和传递),并将这个新配置保存在同一个文件中。

任何人都可以指明方向吗?

最佳答案

这样行吗?

Properties props = new Properties();
FileInputStream fis = new FileInputStream( "database.properties" );
props.load( fis );
fis.close();

props.setProperty("jdbc.url", {{urlvalue}} );
props.setProperty("jdbc.user", {{user value}} );
props.setProperty("jdbc.pass", {{pass value}} );

FileOutputStream fos = new FileOutputStream( "database.properties" );
props.store( fos );
fos.close();

关于java - 从文件加载和保存属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23575583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com