Java 通过从属性文件读取哈希集来填充哈希集
下面是我的Java 通过从属性文件读取哈希集来填充哈希集,java,properties,Java,Properties,下面是我的config.property文件 TABLES: table1 table2 #For Table1 table1.url: jdbc:mysql://localhost:3306/garden table1.user: gardener table1.password: shavel table1.driver: jdbc-driver table1.percentage: 80 table1.column: column1 table1.column: column2 tabl
config.property文件
TABLES: table1 table2
#For Table1
table1.url: jdbc:mysql://localhost:3306/garden
table1.user: gardener
table1.password: shavel
table1.driver: jdbc-driver
table1.percentage: 80
table1.column: column1
table1.column: column2
table1.column: column3
#For Table2
table2.url: jdbc:mysql://otherhost:3306/forest
table2.user: forester
table2.password: axe
table2.driver: jdbc-driver
table2.percentage: 20
table2.column: column1
table2.column: column2
table2.column: column3
下面是我的代码,我试图读取上面的属性文件,并通过使用不同的值填充它来创建ReadTableConnectionInfo
对象,但不知何故,列HashSet并没有填充与每个表对应的所有列名称。对于每个表,我在HashSet的每个列中只看到一个列名
private static void readPropertyFile() throws IOException {
prop.load(Read.class.getClassLoader().getResourceAsStream("config.properties"));
tableNames = Arrays.asList(prop.getProperty("TABLES").split(" "));
for (String arg : tableNames) {
ReadTableConnectionInfo ci = new ReadTableConnectionInfo();
String url = prop.getProperty(arg + ".url");
String user = prop.getProperty(arg + ".user");
String password = prop.getProperty(arg + ".password");
String driver = prop.getProperty(arg + ".driver");
String table = prop.getProperty(arg + ".table");
double percentage = Double.parseDouble(prop.getProperty(arg + ".percentage"));
String columnPrefix = arg + ".column";
HashSet<String> columns = new HashSet<String>();
for (String key : prop.stringPropertyNames()) {
if (key.startsWith(columnPrefix))
columns.add(prop.getProperty(key));
}
ci.setUrl(url);
ci.setUser(user);
ci.setPassword(password);
ci.setDriver(driver);
ci.setTableName(table);
ci.setPercentage(percentage);
ci.setColumns(columns);
tableList.put(arg, ci);
}
}
private static void readPropertyFile()引发IOException{
load(Read.class.getClassLoader().getResourceAsStream(“config.properties”);
tableNames=Arrays.asList(prop.getProperty(“TABLES”).split(“”);
for(字符串参数:表名){
ReadTableConnectionInfo ci=新的ReadTableConnectionInfo();
字符串url=prop.getProperty(arg+“.url”);
字符串user=prop.getProperty(arg+“.user”);
字符串密码=prop.getProperty(arg+“.password”);
字符串驱动程序=prop.getProperty(arg+“.driver”);
字符串table=prop.getProperty(arg+“.table”);
double percentage=double.parseDouble(prop.getProperty(arg+“.percentage”);
字符串ColumnPrifix=arg+“.column”;
HashSet columns=新的HashSet();
for(字符串键:prop.stringPropertyNames()){
if(key.startsWith(columnProfix))
columns.add(prop.getProperty(key));
}
ci.setUrl(url);
ci.setUser(用户);
ci.setPassword(密码);
ci.setDriver(驱动程序);
ci.setTableName(表);
ci.设定百分比(百分比);
ci.setColumns(columns);
tableList.put(arg,ci);
}
}
我在这里填充列HashSet,然后将该列HashSet添加到ReadTableConnectionInfo类中,这有什么不对吗?问题是,您在属性文件中多次重复同一个键,因此在加载它时,属性对象中只加载一个键:
table1.column: column1
table1.column: column2 //key: table1.column
table1.column: column3 //key: table1.column
//similar for table2
只需将密钥名称更改为其他名称
如果您不想这样做,可以将所有值串联在一个键中,然后使用String#split
函数恢复每个值。问题是,您在属性文件中多次重复同一个键,因此在加载该键时,properties
对象中只加载一个键:
table1.column: column1
table1.column: column2 //key: table1.column
table1.column: column3 //key: table1.column
//similar for table2
只需将密钥名称更改为其他名称
如果不想这样做,可以将所有值串联在一个键中,然后使用String#split
函数恢复每个值