Here you can find the source of getBytesArrayFromFile(String fileName)
Parameter | Description |
---|---|
fileName | the file to get the bytes array |
public static byte[] getBytesArrayFromFile(String fileName) throws Exception
//package com.java2s; /*/*from ww w . j ava2 s . c o m*/ * Copyright (c) 2003-2010 The Regents of the University of California. * All rights reserved. * * '$Author: crawl $' * '$Date: 2013-02-21 11:20:05 -0800 (Thu, 21 Feb 2013) $' * '$Revision: 31474 $' * * Permission is hereby granted, without written agreement and without * license or royalty fees, to use, copy, modify, and distribute this * software and its documentation for any purpose, provided that the above * copyright notice and the following two paragraphs appear in all copies * of this software. * * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES, * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, * ENHANCEMENTS, OR MODIFICATIONS. * */ import java.io.FileInputStream; public class Main { /** * Get the bytes array from a file * *@param fileName * the file to get the bytes array *@return he bytes array */ public static byte[] getBytesArrayFromFile(String fileName) throws Exception { byte[] readBytes = new byte[2000]; int n = 0; int i = 0; byte[] totalBytes = null; byte[] tmpBytes = null; int curByteCnt = 0; try { FileInputStream file = new FileInputStream(fileName); n = file.read(readBytes, 0, 2000); curByteCnt = 0; while (n > 0) { if (totalBytes == null) { totalBytes = new byte[n]; for (i = 0; i < n; i++) { totalBytes[i] = readBytes[i]; } } else { tmpBytes = new byte[totalBytes.length]; for (i = 0; i < totalBytes.length; i++) { tmpBytes[i] = totalBytes[i]; } totalBytes = null; totalBytes = new byte[tmpBytes.length + n]; for (i = 0; i < tmpBytes.length; i++) { totalBytes[i] = tmpBytes[i]; } for (i = 0; i < n; i++) { totalBytes[tmpBytes.length + i] = readBytes[i]; } tmpBytes = null; } n = file.read(readBytes, 0, 2000); } file.close(); } catch (Exception e) { throw e; } return totalBytes; } }