我将如何将其从加载 .bin 更改为 .txt。我希望将 .txt 文件的信息像 .bin 文件一样放入哈希图中。
public static int currency;
public HashMap<Integer, Shop> cars = new HashMap<Integer, Car>();
public void load() {
try {
@SuppressWarnings("resource")
RandomAccessFile carFile = new RandomAccessFile("data/cars.bin", "r");
int carsAmt = carFile.readShort();
for (int carId = 0; carId < carAmt; carId++) {
int carId = carFile.readShort();
int currency = carFile.readShort();
int[] cars = new int[carFile.readByte()];
int[] amounts = new int[cars.length];
boolean isTruck = carFile.read() == 1;
for (int carData = 0; carData < cars.length; carData++) {
cars[carData] = carFile.readShort();
amounts[carData] = carFile.readInt();
}
cars.put(carId, new Car(carId, currency, isTruck, cars, amounts, true));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Loaded " + cars.size() + " Cars");
}