common_helper/src/main/java/com/xiongyingqi/util/FileHelper.java
public static byte[] readFileToBytes(File iconFile) throws Exception {
        try {
            validateFile(iconFile);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        long fileSize = iconFile.length();
        if (fileSize > Integer.MAX_VALUE) {
            throw new Exception("读取的文件过大！");
        }
        byte[] data = new byte[(int) fileSize];// 由于文件已经确定，因此大小也可以确定

        try {
            int length = -1;

            FileInputStream inputStream = new FileInputStream(iconFile);
            try {
                int bufferSize = 128;
                byte[] buffer = new byte[bufferSize];
                int offset = 0;
                while ((length = inputStream.read(buffer)) != -1) {
                    // if(length != bufferSize){
                    // System.arraycopy(buffer, 0, data, offset, length);
                    // }
                    System.arraycopy(buffer, 0, data, offset, length);// 从缓冲区拷贝数组
                    offset += length;
                }
            } catch (IOException e) {
                LOGGER.error(e);
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
            return null;
        }
        return data;
    }
public static byte[] readFileToBytes(File iconFile) throws Exception {
        try {
            validateFile(iconFile);
        } catch (Exception e2) {
            e2.printStackTrace();
        }
        long fileSize = iconFile.length();
        if (fileSize > Integer.MAX_VALUE) {
            throw new Exception("读取的文件过大！");
        }
        byte[] data = new byte[(int) fileSize];// 由于文件已经确定，因此大小也可以确定

        try {
            int length = -1;

            FileInputStream inputStream = new FileInputStream(iconFile);
            try {
                int bufferSize = 128;
                byte[] buffer = new byte[bufferSize];
                int offset = 0;
                while ((length = inputStream.read(buffer)) != -1) {
                    // if(length != bufferSize){
                    // System.arraycopy(buffer, 0, data, offset, length);
                    // }
                    System.arraycopy(buffer, 0, data, offset, length);// 从缓冲区拷贝数组
                    offset += length;
                }
            } catch (IOException e) {
                LOGGER.error(e);
            } finally {
                try {
                    inputStream.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
            return null;
        }
        return data;
    }
public static byte [ ] readFileToBytes ( File iconFile ) throws Exception { try { validateFile ( iconFile ) ; } catch ( Exception e2 ) { e2 . printStackTrace ( ) ; } long fileSize = iconFile . length ( ) ; if ( fileSize > Integer . MAX_VALUE ) { throw new Exception ( "读取的文件过大！"); } byte [ ] data = new byte [ ( int ) fileSize ] ; // 由于文件已经确定，因此大小也可以确定 try { int length = - 1 ; FileInputStream inputStream = new FileInputStream ( iconFile ) ; try { int bufferSize = 128 ; byte [ ] buffer = new byte [ bufferSize ] ; int offset = 0 ; while ( ( length = inputStream . read ( buffer ) ) != - 1 ) { // if(length != bufferSize){ // System.arraycopy(buffer, 0, data, offset, length); // } System . arraycopy ( buffer , 0 , data , offset , length ) ; // 从缓冲区拷贝数组 offset += length ; } } catch ( IOException e ) { LOGGER . error ( e ) ; } finally { try { inputStream . close ( ) ; } catch ( IOException e1 ) { e1 . printStackTrace ( ) ; } } } catch ( FileNotFoundException e1 ) { e1 . printStackTrace ( ) ; return null ; } return data ; }
读取文件内容并返回字节数组 <br>
2013-9-3 下午12:10:06

@param iconFile
@return
读取文件内容并返回字节数组 <br > 2013 - 9 - 3 下午12 : 10 : 06