4

I have a .wav file in my disk drive. I need to convert that .wav file to byte array using java.

Can anyone help?

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
user74
  • 51
  • 2
  • 5

2 Answers2

6

You can use Files.readAllBytes to achieve this.

Read all the bytes from a file. The method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137
  • 4
    +1 for pointing out one of the less known new features in Java 7. That said, I am not sure if the OP actually wants the whole file as a byte array, or just the audio... – thkala Dec 10 '12 at 14:05
  • Be careful though, this will read the 44 header bytes too; you'll need to skip them. This method worked for me too. – mindoverflow Nov 16 '17 at 09:57
3

For JRE < 1.7, regardless of extension

File file = new File(filePath);
InputStream fis = new FileInputStream(file);
byte[] buffer = new byte[(int)file.length()];
fis.read(buffer, 0, buffer.length);
fis.close();
Mehmet Ataş
  • 11,081
  • 6
  • 51
  • 78