byte数组转byte数组 java_Java中文件与字节数组转换
Java文件与字节数组转换
/**
* 返回一个byte数组
* @param file
* @return
* @throws IOException
*/
private byte[] getBytesFromFile(File file){
byte[] bytes = null;
try {
InputStream is = new FileInputStream(file);
// 获取文件大小
long length = file.length();
if (length > Integer.MAX_VALUE) {
// 文件太大,无法读取
throw new IOException("File is to large " + file.getName());
}
// 创建一个数据来保存文件数据
bytes = new byte[(int) length];
// 读取数据到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 bytes
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return bytes;
}
/**
* 文件转化为字节数组
*
* @param file
* @return
*/
public static byte[] getBytesFromFile(File file) {
byte[] ret = null;
try {
if (file == null) {
// log.error("helper:the file is null!");
return null;
}
FileInputStream in = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream(4096);
byte[] b = new byte[4096];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
out.close();
ret = out.toByteArray();
} catch (IOException e) {
// log.error("helper:get bytes from file process error!");
e.printStackTrace();
}
return ret;
}
/**
* 把字节数组保存为一个文件
*
* @param b
* @param outputFile
* @return
*/
public static File getFileFromBytes(byte[] b, String outputFile) {
File ret = null;
BufferedOutputStream stream = null;
try {
ret = new File(outputFile);
FileOutputStream fstream = new FileOutputStream(ret);
stream = new BufferedOutputStream(fstream);
stream.write(b);
} catch (Exception e) {
// log.error("helper:get file from byte process error!");
e.printStackTrace();
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
// log.error("helper:get file from byte process error!");
e.printStackTrace();
}
}
}
return ret;
}
byte数组转byte数组 java_Java中文件与字节数组转换相关推荐
- 读入的字节都写入字节数组中_使用Java将文件读入字节数组的7个示例
读入的字节都写入字节数组中 嗨,大家好,Java程序员经常在现实世界中遇到编程问题,他们需要将文件中的数据加载到字节数组中,该文件可以是文本文件或二进制文件. 一个示例是将文件的内容转换为String ...
- java中图片和字节数组相互转化
java中图片和字节数组相互转化 方案一:使用ImageIO来实现 将图片转化为字节数组 /*** 通过图片路径将图片文件转化为字符数组* * @param url 图片路径* @return byt ...
- 用Java将文件读入字节数组的7个示例
嗨,大家好,Java程序员经常在现实世界中遇到编程问题,他们需要将文件中的数据加载到字节数组中,该文件可以是文本或二进制文件. 一个示例是将文件的内容转换为String以便显示. 不幸的是,用于表示文 ...
- java中如何获取字节数组_如何从java中读取字节数组中的数据
大家好 我将一个mp3文件转换为字节数组,我从字节数组中读取,但它显示行号15的空指针异常 我的代码: public class MainClass { static byte[] bytesarra ...
- 数据转换(字节数组转文件、文件转字节数组、16进制字符转字节数组、字节数组转16进制字符)
字节数组转文件 procedure ByteArrayToFile(const ByteArray: TByteDynArray;const FileName: string); varCount: ...
- Java字节数组转文件,文件转字节数组
Java字节数组转文件,文件转字节数组 package IO;import java.io.*;public class IOtest07 {public static void main(Strin ...
- java中字符和字节的转换_Java最佳实践–字符到字节和字节到字符的转换
java中字符和字节的转换 在使用Java编程语言时,我们将继续讨论与建议的实践有关的系列文章,我们将讨论String性能调优. 特别是,我们将着重于使用默认编码时如何有效地处理字符到字节和字节到字符 ...
- java从数据库读取zip文件的字节数组byte[]直接解压
直接上代码 /*** * @param destDir* 存储解压文件的路径* @param fileBytes* 文件字节数组* @throws FileNotFoundException* @th ...
- 转数组_Java中IO必杀技-文件转字节数组
在Java开发中,无论是文件存储还是别的操作,需要经常把文件转化为字节数组,常规的做法就是用内存字节输出流ByteArrayOutPutStream 转化,这样虽然可以实现但是还是有些麻烦,今天教大家 ...
最新文章
- 干货丨详解深度学习的一些关键术语
- 致力云安全服务模式 安全狗获近5000万融资
- HashMap的容量(桶的数量)为什么要是2的n次方
- atmega8 例程:T1定时器 CTC模式 方波输出
- python中mainloop添加背景_Python实例讲解 - tkinter canvas (设置背景图片及文字)
- imageset matlab,如何以imageSet或imageDataStore的形式向MATLAB中的BagOfFeatures()函數提供輸入?...
- In和exists使用及性能分析(一):in的使用
- mysql中的group_MySQL中使用group
- notepad++行首行尾添加字符
- Xgboost中特征重要性计算方法详解
- log4j 日志输出级别区别
- MD5加密算法特点及简单实现(Java)
- 程序员出海创富的机会在哪里?三位专家给出具体建议
- nodejs addon
- java实现两个时间的累加_java List 相邻两个数据累加,可以用stream的collectors.reducing实现么...
- php面向对象之多态
- Android高级工程师技能知识储备,android实战项目源码
- 安装小觅相机(1030)驱动以及如何将Ubuntu18.04的内核降到4.15.0版本
- 系统定制封装-windows
- 论文 计算机动态网页的制作,计算机动态网页设计中多元素应用网页设计论文(范文1)...
热门文章
- c调用其他类的方法_吊打面试官-类加载器
- 【英语学习】【WOTD】smithereens 释义/词源/示例
- python函数做n_【python】定义函数、参数、递归(n!)
- 中软python编码规范考试试题_这些常见的python学习问题,你中枪了吗?
- Unreal Engine 4 —— Ghost Mesh Plugin的开发日志
- jison解析Cube信息 存入又读取出来
- mac 内核调试环境搭建
- 《计算机网络:自顶向下方法(原书第6版)》一第1章
- Mysql数据库Sql语句执行效率-Explain
- js一些平时会用到的