用配置文件获取信息 (通过new property)
package org.example;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class TestProperty {
public static void main(String[] args) {
Properties properties = new Properties();
properties.setProperty("tablename","hello-kitty");
String[] tablenames = properties.getProperty("tablename").split("-");
for (String tablename: tablenames){
System.out.println(tablename);
}
/*获取 配置文件的属性--这样就不必把配置写死,可以灵活的在配置文件统一*/
//文件要放到resource目录下
/*获取资源文件,一般推荐采用这种方式读取*/
InputStream path = Thread.currentThread().getContextClassLoader().getResourceAsStream("message.properties");
Properties newproperties = new Properties();
try {
newproperties.load(path);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(newproperties.getProperty("pg.host"));
System.out.println(newproperties.getProperty("pg.port"));
}
}
文件和结果如下