当前位置:网站首页>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]所创,转载请带上原文链接,感谢
边栏推荐
- VirtualBox install centos7
- net.sf.json . jsonobject's format processing of time stamp
- Express framework
- Come and have a look! What is the relationship between AQS and countdownlatch?
- 选择排序
- Proficient in high concurrency and multithreading, but can't use ThreadLocal?
- Octave基本语法
- 第五章
- Flink series (0) -- Preparation (basic stream processing)
- Infix expression to suffix expression
猜你喜欢
随机推荐
C / C + + learning diary: original code, inverse code and complement code
Mycat搭建
动态规划之子序列问题解题模板
在Python中创建文字云或标签云
Mongodb add delete modify query operation
Dynamic ReLU:微软推出提点神器,可能是最好的ReLU改进 | ECCV 2020
Octave基本语法
简明 VIM 练级攻略
Not a programmer, code can't be too ugly! The official writing standard of Python: pep8 everyone should know
Newbe.ObjectVisitor 样例 1
综合架构的简述
PAT_ Grade A_ 1056 Mice and Rice
Introduction to latex
PAT_甲级_1056 Mice and Rice
RSA非对称加密算法
. net core cross platform resource monitoring library and dotnet tool
程序员都应该知道的URI,一文帮你全面了解
[200 interview experience], programmer interview, common interview questions analysis
Dynamic programming: maximum subarray
Tasks of the first week of information security curriculum design (analysis of 7 instructions)








