Reputation: 18068
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
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
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