使用JAVA将File文件转换为byte[]数组
来源:互联网 发布:工作证在线生成软件 编辑:程序博客网 时间:2024/05/20 22:31
// 返回一个byte数组public static byte[] getBytesFromFile(File file) throws IOException {InputStream is = new FileInputStream(file);// 获取文件大小 long lengths = file.length();System.out.println("lengths = " + lengths);if (lengths > Integer.MAX_VALUE) {// 文件太大,无法读取throw new IOException("File is to large "+file.getName());}// 创建一个数据来保存文件数据byte[] bytes = new byte[(int)lengths];// 读取数据到byte数组中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) {throw new IOException("Could not completely read file "+file.getName());}// Close the input stream and return bytesis.close();return bytes;}
0 0
- 使用JAVA将File文件转换为byte[]数组
- ASP.NET 将文件转换为byte数组
- java中如何将byte数组内容转换为字符串?
- Code1 将字符串转换为byte数组
- Code2 将byte数组转换为字符串
- 将String 转换为byte[]数组
- 将object类型转换为byte[]数组
- 将InputStream转换为byte数组
- java File和Byte[]数组 相互转换
- Java中如何将以byte数组给出的数据转换为double数组形式
- 用java实现将int等类型转换为byte数组,将byte数组转换为int等类型,支持大小端字节序
- C#将int数组转换为byte数组
- Java 将文件转换写入byte[]
- windows mobile平台中使用C#将byte数组转换为Format24bppRgb格式的图片
- JAVA 把File文件转换成Byte[]数组的标准写法
- android 文件上传时将file转化为byte[]
- java 文件和byte数组相互转换
- Java 文件和byte数组转换
- 小白linux bash 学习前奏三 创建用户
- 模拟和仿真
- (未完成)读书笔记--世界运转的大道
- 多重继承和虚继承的内存布局
- 小白linux bash 学习前奏四 历史命令,别名和通配符
- 使用JAVA将File文件转换为byte[]数组
- 类图(Rose) - Windows XP经典软件系列
- asp.net备份还原sql数据库
- Unicode 编码范围和中文编码范围
- Android开发之核心特性SearchView的开发使用(源代码分享)
- 为什么 y<0 时,输出结果不对?
- SpringMvc(4)文件上传
- !hdu1163(求数根)
- ++a和a++的区别