Advertisement
bkit4s0

[ContentToByteArrayExample] read file content in to byte arr

Mar 12th, 2015
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.70 KB | None | 0 0
  1. import java.io.File;
  2. import java.io.FileInputStream;
  3.  
  4. public class ContentToByteArrayExample {
  5.     public static void main(String[] args) {
  6.  
  7.         File file = new File("D:/test.txt");
  8.  
  9.         readContentIntoByteArray(file);
  10.     }
  11.  
  12.     private static byte[] readContentIntoByteArray(File file) {
  13.         FileInputStream fileInputStream = null;
  14.         byte[] bFile = new byte[(int) file.length()];
  15.         try {
  16.             // convert file into array of bytes
  17.             fileInputStream = new FileInputStream(file);
  18.             fileInputStream.read(bFile);
  19.             fileInputStream.close();
  20.             for (int i = 0; i < bFile.length; i++) {
  21.                 System.out.print(/* (char) */bFile[i]);
  22.             }
  23.         } catch (Exception e) {
  24.             e.printStackTrace();
  25.         }
  26.         return bFile;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement