Skip to content

Instantly share code, notes, and snippets.

@werbet
Last active August 29, 2015 14:01
Show Gist options
  • Save werbet/e2f61ef29eebad7e0d45 to your computer and use it in GitHub Desktop.
Save werbet/e2f61ef29eebad7e0d45 to your computer and use it in GitHub Desktop.
How to get a byte array out of a binary file, and copy it into another file, effectively creating a copy of the original file
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class BinaryFileCopy {
public static void main(String[] args) throws IOException {
File file = new File("/home/eriko/Videos/big_buck_bunny_480p_stereo.ogg");
byte[] fileData = new byte[(int) file.length()];
DataInputStream dis = new DataInputStream(new FileInputStream(file));
dis.readFully(fileData);
dis.close();
File outFile = new File("/home/eriko/Videos/copia.ogg");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(outFile));
dos.write(fileData, 0, fileData.length);
dos.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment