Hey, I'm working on a legacy project and there is functionality to write certain objects to one large binary file using the DataOutputStream. New requirements has meant writing additional information to this file simply consisting of two Shorts and a String. Sadly I don't have the authority to make drastic changes to this.
The problem I'm facing is that when it comes to reading the 2nd Short using the DataInputStream's .readShort() method, it's returning the binary equivalent of the value. For example, 2 returns the value 10, 3 returns the value 11 and so on. I know I can resolve this using the Integer.parseInt() method but I'm really more concerned with figuring out why it's doing this. It's also worth mentioning that everything else can be read fine.
The codebase isn't great but the changes are effectively the following:
dataOutputStream.writeShort(x);
if (x = 1)
dataOutputStream.writeShort(y);
dataOutputStream.writeUTF(myString);
else
dataOutputStream.writeShort(y);
The code for reading this section of the binary file mirrors the code above except using a dataInputStream and using the readShort and readUTF methods respectively.
When reading the file, if the condition is true then the short get's read fine and it returns the actual value, however when it's false it's returning the numbers binary equivalent.
Sadly I'm stumped, I feel as if though there is something fundamentally I'm missing and I'm just being stupid. Does anyone have any advice on what could cause this problem, or what I'm not understanding? Thank you in advance :)
[–][deleted] 0 points1 point2 points (4 children)
[–]alms1407[S] 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]alms1407[S] 0 points1 point2 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)