我有一个用于我正在更新的DOS程序的配置文件。 。 。从数组读取字节数组到Java数组
配置文件为17512字节。前128个字节是标题信息,其余字节被分成256个记录,每个记录64个字节。每个记录包含名称(8字节),描述(18字节),单元号(1字节)等设备的特定信息。我正在将文件读取到一个大字节数组中,然后想要提取单个设备信息,以便可以在新的GUI界面中对其进行编辑。
我创建了一个包含设备名称字段的Device类。我想创建一个包含所有256个设备的类的数组,但是当我尝试读取各个设备名称时,所有256个设备都会以最后读取的设备结束。我不确定我出错的地方。
这里是Main.java
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
ReadConfigFile.importConfigFile(); // Read config file into byte array.
Device[] device = new Device[256]; // Create array of 256 Devices.
device[0].code = Device.setCode(0);
System.out.println(new String(device[0].code)); // First device correct here.
device[255].code = Device.setCode(255);
System.out.println(new String(device[0].code)); // First device now same as last?
System.out.println(new String(device[255].code));
Group root = new Group();
Scene scene = new Scene(root, 200, 200);
primaryStage.setTitle("Config File Editor");
primaryStage.setScene(scene);
primaryStage.show();
}
}
的代码下面是该类设备的代码。到目前为止,我只有第一个领域。当我能够正常工作时,我会添加其余的。
public class Device {
public static byte[] code; // 8 bytes. (1 - 8).
public Device() {
code = new byte[8]; // Constructor correct?
}
public static byte[] setCode(int devNumber) {
int devCodeByteStart = (128 + (64 * devNumber)); // Skip first 128 bytes to get to first device.
int devCodeByteStop = (devCodeByteStart + 8); // Get 8 bytes for device code.
byte[] code = new byte[8]; // Gives Null Pointer Exception if removed.
for(int byteCount = devCodeByteStart; byteCount < devCodeByteStop; byteCount++) {
code[byteCount - devCodeByteStart] = configFileBytes[byteCount];
}
return code;
}
}
如果有更好的方法来完成任务,我愿意接受建议。
你知道什么是'static'意味着/呢? – Amit
@Amit haha ...这么简单... :) –
Device的所有实例共享相同的代码数组,因为您声明为静态。 – JJF