Skip to content

Instantly share code, notes, and snippets.

@roopeshvaddepally
Created March 15, 2012 20:17
Show Gist options
  • Save roopeshvaddepally/2046626 to your computer and use it in GitHub Desktop.
Save roopeshvaddepally/2046626 to your computer and use it in GitHub Desktop.
Get ByteArray of file content (don't you on large files)
public static byte[] getFileBytes(File file) throws IOException {
ByteArrayOutputStream ous = null;
InputStream ios = null;
try {
byte[] buffer = new byte[4096];
ous = new ByteArrayOutputStream();
ios = new FileInputStream(file);
int read = 0;
while ((read = ios.read(buffer)) != -1)
ous.write(buffer, 0, read);
} finally {
try {
if (ous != null)
ous.close();
} catch (IOException e) {
// swallow, since not that important
}
try {
if (ios != null)
ios.close();
} catch (IOException e) {
// swallow, since not that important
}
}
return ous.toByteArray();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment