-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCopy.java
More file actions
35 lines (29 loc) · 1.04 KB
/
Copy pathFileCopy.java
File metadata and controls
35 lines (29 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package io;
import java.io.*;
public class FileCopy {
public static void main(String[] args) {
// TODO自动生成的方法存根
byte[] buffer=new byte[512]; //一次取出的字节数大小,缓冲区大小
int numberRead=0;
InputStream input=null;
OutputStream out =null;
try {
input=new FileInputStream("img/11.gif");
out=new FileOutputStream("img/2.png"); //如果文件不存在会自动创建
while ((numberRead=input.read(buffer))!=-1) { //numberRead的目的在于防止最后一次读取的字节小于buffer长度,
out.write(buffer, 0, numberRead); //否则会自动被填充0
}
} catch (final IOException e) {
// TODO自动生成的 catch 块
e.printStackTrace();
}finally{
try {
input.close();
out.close();
} catch (IOException e) {
// TODO自动生成的 catch 块
e.printStackTrace();
}
}
}
}