字节流拷贝文件

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
36
37
38
39
40
41
42
public class  {

public static void main(String[] args) {
File f_s = new File("d:\2.txt");
File f_d = new File("e:/yxm"); // 要拷贝到的路径
if (!f_d.exists()) { // 判断要拷贝到的路径是否存在,不存在就创建
f_d.mkdirs();

}
File f_dd = new File(f_d, "7s.txt");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(f_s);
fos = new FileOutputStream(f_dd);
byte[] byteTemp = new byte[1024];
while(fis.read(byteTemp) != -1){
int size =byteTemp.length;
fos.write(byteTemp,0,size);

}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
if (fis != null) {
fos.close();

}
if (fos != null) {
fis.close();

}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}