Okay guys, I have a file with some HEX values as well as a program that take this values with a byte[] in order to convert some hex values and then reconvert it to a file. The problem is that when I reconvert de byte array to a file some hex values are modified, and I don't find the problem. If you see any possible mistake don't hesitate.
As you can see I have a test.sav file, here it is:

And this is the product of the program, the two files are different and they should be the same because any change has been made:

Here is the code:
public class Test {
public static File file;
public static String hex;
public static byte[] mext;
public static byte[] bytearray;
public static void main(String[] args) throws IOException {
file = new File("C:\\Users\\Roman\\Desktop\\test.sav");
StringBuilder sb = new StringBuilder();
FileInputStream fin = null;
try {
fin = new FileInputStream(file);
bytearray = new byte[(int)file.length()];
fin.read(bytearray);
for(byte bytev : bytearray){
sb.append(String.format("%02X", bytev));
}
System.out.println(sb);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {}
//replaceMax(); <-- I deduced that conversion is not the problem
save(); // THIS IS THE IMPORTANT PART
}
public static void save() throws IOException{
PrintWriter pw = new PrintWriter("C:\\Users\\Roman\\Desktop\\test2.sav");
pw.write("");
pw.close();
FileWriter fw = new FileWriter(new File("C:\\Users\\Roman\\Desktop\\test2.sav"));
BufferedWriter out = new BufferedWriter(fw);
out.write(new String(bytearray, "ASCII"));
out.close();
}
}
FileInputStream.readcorrectly. It might not return you all of the data in case I/O blocks. Check out the documentation.readand if it is less thanbytarray.length, callreadagain for the remaining bytes until finally the entire array is eventually read. You'll want to switch to using the overload ofreadthat accepts an offset as additional parameter.