Java 属性元素

Java properties elements

我目前正在开发一个应用程序,该应用程序将用于通过 SSH 为 on/off 设备(服务器)供电。现在我使用具有以下设置的 属性 文件:

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 循环所有属性。

创建一个名为

的 class
public class MyProperties{

private String key;
private String value;
//get/set() methods of key/value OR Create Constructor with Varaibles
}

现在写这个逻辑

 private List<MyProperties> createPropertyList(){
    Properties proObj = new Properties();
    proObj.load(new FileInputStream("PathOfPropertiesFile"));
    List<MyProperties> propertyList = new ArrayList<MyProperties>();
    for(String propertyKey: proObj.stringPropertyNames()) {
      MyProperties myProperties = new MyProperties();
      myProperties.setKey(propertyKey);
      myProperties.setValue(properties.getProperty(propertyKey));
      propertyList.add(myProperties);
    }
  return propertyList;
}

以下代码示例将在映射中添加请求的输出对象,并将两个所需命令(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}

看看ResourceBundle class here.

getKeys() 方法将 return String 的枚举。 您还可以使用 getString(key) 方法获取值。它接受一个键作为参数。