Created
March 15, 2012 20:17
-
-
Save roopeshvaddepally/2046626 to your computer and use it in GitHub Desktop.
Get ByteArray of file content (don't you on large files)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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