File to byte array


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. private static byte[] fileToByteArray(String fileName) throws Exception {
  2. FileInputStream fis = new FileInputStream(fileName);
  3.  
  4. byte buffer[] = new byte[4096];
  5. int length = -1;
  6.  
  7. while (true) {
  8. length = fis.read(buffer);
  9. if (length == -1){
  10. break;
  11. }
  12. baos.write(buffer, 0, length);
  13. }
  14.  
  15. fis.close();
  16.  
  17. return baos.toByteArray();
  18. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.