当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- APReLU:跨界应用,用于机器故障检测的自适应ReLU | IEEE TIE 2020
- LeetCode 45 跳跃游戏II
- PAT_甲级_1056 Mice and Rice
- 信息安全课程设计第一周任务(7条指令的分析)
- Opencv solves the problem of ippicv download failure_ 2019_ lnx_ intel64_ general_ 20180723.tgz offline Download
- C / C + + knowledge sharing: function pointer and pointer function, can you understand after reading this article?
- 使用基于GAN的过采样技术提高非平衡COVID-19死亡率预测的模型准确性
- 给大家介绍下,这是我的流程图软件 —— draw.io
- 第五章
- JVM真香系列:轻松理解class文件到虚拟机(上)
猜你喜欢
The minimum insertion times of palindrome
Why need to use API management platform
采用注解+拦截器的方式进行异步执行的实现方式
微信小程序相关
综合架构的简述
Using annotation + interceptor to implement asynchronous execution
解决IE、firefox浏览器下JS的new Date()的值为Invalid Date、NaN-NaN的问题
Learn volatile, you change your mind, I see
快来看看!AQS 和 CountDownLatch 有怎么样的关系?
简明 VIM 练级攻略
随机推荐
MongoDB数据库
Mongodb add delete modify query operation
getBytes之 LengthFieldBasedFrameDecoder服务端解析
Countdownlatch explodes instantly! Based on AQS, why can cyclicbarrier be so popular?
Programmers should know the URI, a comprehensive understanding of the article
C/C++学习日记:原码、反码和补码
C/C++知识分享: 函数指针与指针函数,看完这篇你还能不懂?
国内三大云数据库测试对比
Creating a text cloud or label cloud in Python
ITerm2 配置和美化
opencv 解决ippicv下载失败问题ippicv_2019_lnx_intel64_general_20180723.tgz离线下载
MYCAT build
用两个栈实现队列
[elastic search technology sharing] - ten pictures to show you the principle of ES! Understand why to say: ES is quasi real time!
综合架构的简述
程序员都应该知道的URI,一文帮你全面了解
采用注解+拦截器的方式进行异步执行的实现方式
计算机网络 应用层
C / C + + learning diary: original code, inverse code and complement code
在Python中创建文字云或标签云