2

I want to read all bytes on a file, but when I do this

Path fileLocation = Paths.get("./env.wav");
byte[] data = Files.readAllBytes(fileLocation);
System.out.println(data);

And it outputs only this:

[B@6ab1bd82

Instead of outputting a byte array like this:

249 4646 ac98 0200 5741 5645 666d 7420 1000 0000 0100 0100 44ac 0000 8858 0100 0200 1000 6461 7461 8898 0200 7900 5200 5600 3b00 3100 0c00 6500 4000 2500 7a00 2d00 0c00 5400 5100 2500 1200 feff 0d00 [etc..............]

Crearo Lisifi
  • 47
  • 1
  • 5

2 Answers2

3

Use Arrays.toString(). Printing the array prints its toString() which defaults to Object's toString().

Path fileLocation = Paths.get("./env.wav");
byte[] data = Files.readAllBytes(fileLocation);
System.out.println(Arrays.toString(data));
Sunny Agarwal
  • 1,451
  • 4
  • 18
  • 36
0

You are printing the array directly. The currently printed value is the memory location of the array.

If you want the content of the array you will need to cast it to something printable or loop over the content and print each byte (cast each byte into a printable value).

One way would be: how to print the data in byte array as characters

Community
  • 1
  • 1
monty
  • 7,888
  • 16
  • 63
  • 100
  • 1
    Just mark the question as duplicate, instead of making an answer with a link to the duplicate question. – marstran Jan 17 '17 at 08:21