Advertisement
Guest User

Java001

a guest
Nov 26th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. package test;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7.  
  8. import javax.swing.JOptionPane;
  9.  
  10.  
  11.  
  12.     public class BytesExample
  13.     {
  14.     public static byte[] getBytesFromFile(File file) throws IOException {
  15.         InputStream fileInputStream = new FileInputStream(file);
  16.         // Get the size of the file
  17.         long length = file.length();
  18.         // You cannot create an array using a long type.
  19.         // It needs to be an int type.
  20.         // Before converting to an int type, check
  21.         // to ensure that file is not larger than Integer.MAX_VALUE.
  22.         if (length > Integer.MAX_VALUE) {
  23.            
  24.         }
  25.         // Create the byte array to hold the data
  26.         byte[] bytes = new byte[(int)length];
  27.         // Read in the bytes
  28.         int offset = 0;
  29.         int numRead = 0;
  30.         while (offset < bytes.length
  31.                && (numRead=fileInputStream.read(bytes, offset, bytes.length-offset)) >= 0) {
  32.             offset += numRead;
  33.         }
  34.         // Ensure all the bytes have been read in
  35.         if (offset < bytes.length) {
  36.             throw new IOException("Could not completely read file "+file.getName());
  37.         }
  38.         // Close the input stream and return bytes
  39.         fileInputStream.close();
  40.         return bytes;
  41.         }
  42.         public static String getHexString(byte[] b) throws Exception
  43.         {
  44.             String result = "";
  45.             for (int i=0; i < b.length; i++)
  46.             {
  47.                     result +=
  48.                     Integer.toString( ( b[i] & 0xff ) + 0x100, 16).substring( 1 );
  49.             }
  50.         return result;
  51.         }
  52.         public static void main(String args[])
  53.         {
  54.             try
  55.             {
  56.                 File file = new File("output.txt");
  57.                 byte[] arrayOfBytes = getBytesFromFile(file);
  58.                
  59.                 System.out.println(Integer.toBinaryString( (int) arrayOfBytes[1]));
  60.                 System.out.println(getHexString(arrayOfBytes));
  61.             }
  62.             catch (IOException e)
  63.             {
  64.                 JOptionPane.showMessageDialog(null,
  65.                          e.getMessage() +
  66.                                "\nError while dealing with files" ,
  67.                                    "\n... ",
  68.                                        JOptionPane.PLAIN_MESSAGE);
  69.             }
  70.             catch (Exception e)
  71.             {
  72.                 JOptionPane.showMessageDialog(null,
  73.                          e.getMessage() +
  74.                                "\nKError while converting bytes to String" ,
  75.                                    "\n... ",
  76.                                        JOptionPane.PLAIN_MESSAGE);
  77.             }
  78.         }                
  79.        
  80.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement