I’ve used this topic: File to byte[] in Java
Here is my code:
try {
Path path1 = Paths.get(path + fileName);
byte[] fileAsByte = Files.readAllBytes(path1);
System.out.println("Byte : " + fileAsByte.toString());
} catch (FileNotFoundException e) {
System.out.println("File Not Found.");
e.printStackTrace();
} catch (IOException e1) {
System.out.println("Error Reading The File.");
e1.printStackTrace();
} catch (OutOfMemoryError e3) {
System.out.println("Out of Memory");
e3.printStackTrace();
}
This code is not triggering any exception, but the output is still:
Byte : [B@60f17a2f
Which seems pretty invalid to me. I’m pretty sure I did a dumb error, but it’s been three hours that I’ve been trying to resolve it, and I could use some fresh eyes on it.
Thanks.
You can’t convert an array directly to
Stringand have it readable by the human eye. It is printing out[(meaning “array”), thenB(forbyte), then@and its identity hash code.To get a list of the bytes in the array, use the static
Arrays.toString()method instead:(If the bytes represent characters for an output string, use @iTech‘s solution.)