我目前正在开发一个用于通过SSH开启/关闭设备(服务器)的应用程序。现在,我使用具有以下设置的属性文件:
command.power_on.name = Power on
command.power_on.host = host.com
command.power_on.user = user
command.power_on.password = password
command.power_on.port = 22
command.power_on.timeout = 10000
command.power_on.command = power on command
command.power_off.name = Power off
command.power_off.host = host.com
command.power_off.user = user
command.power_off.password = password
command.power_off.port = 22
command.power_off.timeout = 10000
command.power_off.command = power off command
我想遍历所有命令,并将它们放入列表,数组,对象,自定义对象或任何可能的对象中。
PSEUDO代码:
String[] commands = propertiesConfiguration.getKeys("command");
for (String command : commands) {
CommandModel[] commandModel = command;
/* Will return two command models with the power_on and power_off attributes.
(name, host, user, password, port, timeout and command) */
}
结果,我想得到像这样的东西(如XML):
Command[] command; // Array with all the commands.
System.out.println(command[0].toString()); // Will print the "power_on" attributes.
System.out.println(command[1].toString()); // Will print the "power_off" attributes.
CommandModel commandModel = new CommandModel(); // The command model with queries (select, select all and update).
Command powerOn = commandModel.getCommand("power_on"); // Will return the command "power_on" with their attributes (name, host, user, password, port, timeout, command).
Command powerOff = commandModel.getCommand("power_off"); // Will return the command "power_off" with their attributes (name, host, user, password, port, timeout, command).
Command[] command = commandModel.getAll(); // Will return an array of all the commands.
我知道这对于基于XML的结构是可行的,但是对于基于属性的文件也可行吗?我只有2个命令,我认为一个属性文件就足够了。还是应该使用XML文件?
欢迎所有反馈,建议等。先感谢您!
最佳答案
以下代码示例将请求的输出对象添加到映射中,并以两个所需命令(poweroff和poweron)的名称作为键。使用反射设置字段。
考虑到属性文件位于应用程序运行所在的文件夹中,以下代码示例将读取属性文件中定义的所有属性。
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class Main {
public static void main(String[] args) throws IOException, NoSuchFieldException, IllegalAccessException {
InputStream is = Main.class.getResourceAsStream("command.properties");
Properties properties = new Properties();
// the output map that will contain the results
// the poweroff related content for key "poweroff"
// the poweron related content for key "poweron"
Map<String, Command> commands = new HashMap<>();
properties.load(is);
// parse the properties file
for (Map.Entry<Object, Object> e :properties.entrySet()) {
// extract the name of the object (poweroff or poweron)
String name = name((String)e.getKey());
// extract the name of the property to be set (timeout, host etc.)
String property = property((String)e.getKey());
// get the object where to set the outcome
Command command = commands.get(name);
// create the object if it wasn't already created
// and add it to the map
if (command == null) {
command = new Command();
commands.put(name, command);
}
// set the value of the given property on the corresponding object
setValue(Command.class, command, property, e.getValue());
}
System.out.println(commands);
}
static String name(String input) {
return input.split("\\.")[1];
}
static String property(String input) {
return input.split("\\.")[2];
}
static void setValue(Class<?> clazz, Object object, String propertyName, Object value) throws NoSuchFieldException, IllegalAccessException {
Field field = clazz.getDeclaredField(propertyName);
field.setAccessible(true);
field.set(object, value);
}
static class Command {
String name;
String host;
String user;
String password;
String port;
String timeout;
String command;
@Override
public String toString() {
return name + " " + host + " " + user + " " + password + " " + port + " " + timeout + " " + command;
}
}
输出:
{power_off=Power off host.com user password 22 10000 power off command, power_on=Power on host.com user password 22 10000 power on command}
关于java - Java属性元素,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32139038/