Yoda
Yoda

Reputation: 18068

How to represent sound file as byte array in Java

I am using some algorithms which need data represented in array of bytes it is no problem when I am working on text files but I don't know how to put a sound file in a that kind of array. I don't have specified extension of that sound file, so it can be whatever is the easiest. So: How to put a sound file to a byte array in Java?

Thank you.

Upvotes: 1

Views: 2305

Answers (2)

Jon Lin
Jon Lin

Reputation: 143886

So: How to put a sound file to a byte array in Java?

File f = new File(soundFilePath, soundFileName);
byte[] soundFileByteArray = new byte[f.length()];
FileInputStream fis = new FileInputStream(f);
fis.read(soundFileByteArray);

Upvotes: 3

gigadot
gigadot

Reputation: 8969

Alternatively, if you can use Apache common IO library, the FileUtils class contains a readFileToByteArray method for reading a file into a byte array in one line:

byte[] bytes = FileUitls.readFileToByteArray(file);

Upvotes: 3

Related Questions