creating objects at runtime from a config file in Java

时间:2015-05-24 22:04:14

标签: java android

I am writing an App for Android where I allow users to create their vehicles, which I store to a config file in a structured format. Something like:

[VEHICLE1]
VEHICLE1>type>car
VEHICLE1>fuelUsageMPG>30
VEHICLE1>speed>100
[ENDVEHICLE1]
[VEHICLE2]
VEHICLE2>type>car
VEHICLE2>fuelUsage>35
VEHICLE2>speed>85
[ENDVEHICLE2]

My App has a class named "vehicle" with the same fixed number of attributes. So what I am trying to do is to create those objects at run-time and set the object properties based on the config file so that I can then use further along in the program to perform calculations.

I parse that config file using flags and referencing groups and cycle through as many times as I find the pattern "[VEHICLE.]" but cannot figure out how to create an instance of vehicles() using VEHICLE1, VEHICLE2 or any other automated incremental object reference.

Any sample code or guidance would be much appreciated.

2 个答案:

答案 0 :(得分:1)

At first, you can simplify your your config file like this:

[VEHICLE1]
    type>car
    fuelUsageMPG>30
    speed>100
[ENDVEHICLE1]
[VEHICLE2]
    type>car
    fuelUsage>35
    speed>85
[ENDVEHICLE2]`

(but I think using a JSON or XML will be easier, as you will not have to use your own parser)

then you can just read from this file vehicle by vehicle and call a constructor of your vehicles for each of them.

Something like: new Vehicle(type, fuelUsage, speed)

EDIT:

You can store them like that:

ArrayList al = new ArrayList();

..... loop start
al.add(new Vehicle(type, fuelUsage, speed));
..... loop end

//get the VEHICLE1
al.get(0);
//get the VEHICLE5
al.get(4);

答案 1 :(得分:1)

我也完全同意AndriiElDuderino,但是如果您无法更改输入格式。您可以尝试以下代码:

如下所示,请使用方法说getVehicleList

public ArrayList<Vehicle> getVehicleList(File fin) {
    ArrayList<Vehicle> vehicleList = new ArrayList<Vehicle>();

    // Taken for my convenience, you should read it from "File fin"
    InputStream ins = getResources().openRawResource(
            getResources().getIdentifier("raw/vehicle", "raw", getPackageName()));
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(ins));
        String line = null;

        int cursor = 0;
        while ((line = br.readLine()) != null) {
            if (line.contains("[VEHICLE")) {
                Vehicle v = new Vehicle();
                vehicleList.add(cursor, v);
            } else if (line.contains("[ENDVEHICLE")) {
                cursor++;
            } else {
                // read vehicle attributes here
                Vehicle v1 = vehicleList.get(cursor);
                String[] attr = line.split(">");
                if (attr != null && attr.length == 3) {
                    if (attr[1].contains("type")) {
                        v1.type = attr[2];
                    } else if (attr[1].contains("fuelUsage")) {
                        v1.fuelUsageMPG = attr[2];
                    } else if (attr[1].contains("speed")) {
                        v1.speed = attr[2];
                    }
                }
            }
        }
        return vehicleList;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

请问会有很多边缘情况,但您的想法是必须创建ArrayList,直到您阅读相同的Vehicle,保持cursor相同并阅读相同的Vehicle 1}}来自cursor的{​​{1}}位置。

相关问题