当前位置:网站首页>Copy the picture
Copy the picture
2020-11-08 21:03:00 【8Years】
public class TestBytesInputStream {
public static void main(String[] args) {
// Copy a.jpg picture
byte[] datas=fileToBytesArray("D:\\a.jpg");
byteArrayToFile(datas,"D:\\b.jpg");
}
//1. The image is read into the byte array
// 1. Picture to program ——FileInputStream
// 2. Program to byte array ——byteArrayOutputStream
public static byte[] fileToBytesArray(String path) {
// Create source and destination
File src = new File(path);
byte[] dest = null;
// Select flow
InputStream is = null;
ByteArrayOutputStream baos = null;
try {
// You can also use is = new BufferedInputStream(FileInputStream(src));
is = new FileInputStream(src);
// You can also use baos = new BufferedOutputStream(teArrayOutputStream());
baos = new ByteArrayOutputStream();
//3. Segment read
byte[] flush = new byte[1024 * 10];// Buffer container
int len = -1;
while ((len = is.read(flush)) != -1) {
baos.write(flush, 0, len);// Write to a byte array
}
baos.flush();
return baos.toByteArray();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != is) {
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public static void byteArrayToFile(byte[]src,String path){
File dest=new File(path);
InputStream is=null;
OutputStream os=null;
try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(dest);
byte[] flush = new byte[5];
int len = -1;
while ((len = is.read(flush)) != -1) {
os.write(flush, 0, len);
}
}catch (IOException e) {
e.printStackTrace();
} finally {
try{
if(os!=null){
os.close();
}
}catch (IOException e) {
e.printStackTrace();
}
}
}
}
It's worth reminding , When is.read() When no parameters are passed in , The copied image will not open , Because it can only read byte by byte , So it's better to pass in the array
Two places to improve performance (1. The use of buffer containers ( It's like a pickup truck ) 2. You can also use byte buffer streams ( It's like a big truck ))
版权声明
本文为[8Years]所创,转载请带上原文链接,感谢
边栏推荐
- 使用Fastai开发和部署图像分类器应用
- Django之简易用户系统(3)
- The interface testing tool eolinker makes post request
- Come and have a look! What is the relationship between AQS and countdownlatch?
- 使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
- C/C++学习日记:原码、反码和补码
- If the programming language as martial arts unique! C++ is Jiu Yin Jing. What about programmers?
- latex入门
- Flink series (0) -- Preparation (basic stream processing)
- 接口测试工具Eolinker进行post请求
猜你喜欢
JVM真香系列:轻松理解class文件到虚拟机(下)
Tasks of the first week of information security curriculum design (analysis of 7 instructions)
给大家介绍下,这是我的流程图软件 —— draw.io
To introduce to you, this is my flow chart software—— draw.io
信息安全课程设计第一周任务(7条指令的分析)
Proficient in high concurrency and multithreading, but can't use ThreadLocal?
VirtualBox安装centos7
第五章
Solve the problem that the value of new date() of JS in IE and Firefox is invalid date and Nan Nan
装饰器(一)
随机推荐
C/C++知识分享: 函数指针与指针函数,看完这篇你还能不懂?
abp(net core)+easyui+efcore实现仓储管理系统——出库管理之五(五十四)
AI人工智能编程培训学什么课程?
Using GaN based oversampling technique to improve the accuracy of model for mortality prediction of unbalanced covid-19
CMS垃圾收集器
Flink series (0) -- Preparation (basic stream processing)
实现图片的复制
精通高并发与多线程,却不会用ThreadLocal?
opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
Learn volatile, you change your mind, I see
Newbe.ObjectVisitor 样例 1
存储过程动态查询处理方法
Django之简易用户系统(3)
为什么需要使用API管理平台
CountDownLatch 瞬间炸裂!同基于 AQS,凭什么 CyclicBarrier 可以这么秀?
Why need to use API management platform
SQL 速查
Express框架
[200 interview experience], programmer interview, common interview questions analysis
经典动态规划:最长公共子序列