码迷,mamicode.com
首页 > 其他好文 > 详细

将文件转成byte[]文件属组

时间:2016-02-17 17:19:23      阅读:120      评论:0      收藏:0      [点我收藏+]

标签:

/**
     * 
     *  @Description    : 读取文件数组
     *  @Method_Name    : fileBuff
     *  @param filePath
     *  @return
     *  @throws IOException
     *  @return         : byte[]
     *  @Creation Date  : 2015年1月27日 下午5:26:49 
     *  @Author         : 
     */
    public static byte[] fileBuff(String filePath) throws IOException {  
        File file = new File(filePath);  
        long fileSize = file.length();  
        if (fileSize > Integer.MAX_VALUE) {  
            //System.out.println("file too big...");  
            return null;  
        }  
        FileInputStream fi = new FileInputStream(file);  
        byte[] file_buff = new byte[(int) fileSize];  
        int offset = 0;  
        int numRead = 0;  
        while (offset < file_buff.length && (numRead = fi.read(file_buff, offset, file_buff.length - offset)) >= 0) {  
            offset += numRead;  
        }  
        // 确保所有数据均被读取  
        if (offset != file_buff.length) {  
        throw new IOException("Could not completely read file "  
                    + file.getName());  
        }  
        fi.close();  
        return file_buff;  
    }

 

将文件转成byte[]文件属组

标签:

原文地址:http://www.cnblogs.com/yy123/p/5195845.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!