私は更新しているDOSプログラムの設定ファイルを持っています。 。 。配列からJavaクラスの配列にバイトを読み込む
設定ファイルは17512バイトです。最初の128バイトはヘッダー情報で、残りのバイトはそれぞれ64バイトの256レコードに分割されます。各レコードには、名前(8バイト)、説明(18バイト)、ユニット番号(1バイト)などのデバイスに関する特定の情報が含まれています。私は大きなバイト配列にファイルを読み込んでいて、個々のデバイス情報を取り出して、新しいGUIインターフェースで編集できるようにしたい。
デバイス名のフィールドを含むデバイスクラスを作成しました。このクラスの配列を作成して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
@All haha ... so simple ... :) –
静的と宣言しているため、Deviceのすべてのインスタンスは同じコード配列を共有します。 – JJF