Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐:
搜索:
您的位置: Linux安全网 > Linux集群 > Architecture > » 正文

文件转换为字节数组

来源: chxiaowu 分享至:
/**
	 * 文件转换为字节数组
	 * @param file
	 * @return
	 * @author ChenHua
	 * create on 2011-12-9  下午1:40:14
	 */
	public  byte[] getBytesFromFile(File file) {  
		
              try {        	
        	    FileInputStream is = new FileInputStream(file);
        	    long length = file.length();
        	    if (length > Integer.MAX_VALUE) {
        	    	 return null;  
        	    }
        	    byte[] bytes = new byte[(int)length];

        	    int offset = 0;
        	    int numRead = 0;
        	    while (offset < bytes.length && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        	        offset += numRead;
        	    }
        	    if (offset < bytes.length) {
        	    	return null;  
        	    }
        	    is.close();
        	    return bytes;
        } catch (IOException e) {  
            e.printStackTrace();  
            return null;  
        }
    }

转载,不知道出处。


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史