我目前正在开发一个应用程序,该应用程序将用于通过 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
我想循环遍历所有命令并将它们放入列表、数组、对象、自定义对象或任何可能的对象中。
伪代码:
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 文件?
欢迎所有反馈、建议等。预先感谢您!
最佳答案
您可以使用 Properties 循环所有属性类(class)。
关于Java 属性元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32139038/