Search code examples
javaaudiobinarywave

Convert .wav file to binary and then back to .wav?


I'm doing a project in java which requires me to encrypt a wave file. So, is there a straight forward process to convert a wave file into binary and back? I'll be applying an encryption algorithm on the binary data.


Solution

  • Yes.

    File file = new File("music.wav");
    byte[] data = new byte[file.length()];
    FileInputStream in = new FileInputStream(file);
    in.read(data);
    in.close();
    
    //encrypt data
    
    FileOutputStream out = new FileOutputStream(file);
    out.write(data);
    out.close();
    

    Of course assuming it's still a valid wav file after you play around with the data.