Source Code : Load file to byte array

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Load file to byte array

       

/******************************************************************
*
* CyberUtil for Java
*
* Copyright (C) Satoshi Konno 2002-2003
*
* File: FileUtil.java
*
* Revision:
*
* 01/03/03
*   - first revision.
*
******************************************************************/


import java.io.*;

public final class FileUtil
{
  public final static byte[] load(String fileName)
  {
    try { 
      FileInputStream fin=new FileInputStream(fileName);
      return load(fin);
    }
    catch (Exception e) {
 
      return new byte[0];
    }
  }

  public final static byte[] load(File file)
  {
    try { 
      FileInputStream fin=new FileInputStream(file);
      return load(fin);
    }
    catch (Exception e) {
     
      return new byte[0];
    }
  }

  public final static byte[] load(FileInputStream fin)
  {
    byte readBuf[] = new byte[512*1024];
  
    try { 
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
    
      int readCnt = fin.read(readBuf);
      while (0 < readCnt) {
        bout.write(readBuf, 0, readCnt);
        readCnt = fin.read(readBuf);
      }
      
      fin.close();
      
      return bout.toByteArray();
    }
    catch (Exception e) {
     
      return new byte[0];
    }
  }
  

}

   
    
    
    
    
    
    
  

Thank with us