Saving an array to a buffer.
Currently in my Java Project, the item bonuses are stored in a buffered file and read like this:
Code :
private static final void loadPackedBonuses() {
try {
RandomAccessFile in = new RandomAccessFile(PACKED_PATH, "r");
FileChannel channel = in.getChannel();
ByteBuffer buffer = channel.map(MapMode.READ_ONLY, 0,
channel.size());
itemBonuses = new HashMap<Integer, int[]>(buffer.remaining() / 38);
while (buffer.hasRemaining()) {
int itemId = buffer.getShort() & 0xffff;
int[] bonuses = new int[18];
for (int index = 0; index < bonuses.length; index++)
bonuses[index] = buffer.getShort();
itemBonuses.put(itemId, bonuses);
}
channel.close();
in.close();
} catch (Throwable e) {
Logger.handle(e);
}
}
Now, I unpacked that into a .txt file and I'm stuck on writing what I unpacked back into a buffered file. The part that I'm stuck on is writing the array of bonuses. Here's what an item bonus line looks like:
Code :
16183 - -4 -4 167 -4 0 0 0 0 -1 1 0 0 0 0 160 0 0 0
Here's my code so far:
Code :
private static final void loadUnpackedBonuses() {
try {
DataOutputStream out = new DataOutputStream(new FileOutputStream(
PACKED_PATH));
BufferedReader in = new BufferedReader(
new FileReader("data/items/itembonuses.txt"));
String line;
while ((line = in.readLine()) != null) {
String[] data = line.split(" - ");
if (data == null)
continue;
int item = Integer.parseInt(data[0]);
int[] bonuses = new int[18];
out.writeShort(item);
for (int i = 0; i < bonuses.length; i++) {
out.writeShort(bonuses[i]);
}
}
in.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
All and any help is appreciated.
Re: Saving an array to a buffer.
Can you explain what the difference is between the contents of a buffered file and a .txt file?
Quote:
part that I'm stuck on is writing the array of bonuses
What does the current code write to the file?
One problem I see in the code is that the array: bonuses never has any data put into it. It will be full of 0s.
Re: Saving an array to a buffer.
There seems to be lot of problems..
in second code can never be null.
What is BufferedFile(do you mean to have some array for holding up the data) and what is the data format in the text file you are reading in second part of code..there seems to error in parsing logic..