字符流拷贝文件

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
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");
FileReader fis = null;
FileWriter fos = null;
try {
fis = new FileReader(f_s);
fos = new FileWriter(f_dd);
char[] charTemp = new char[1];
while (fis.read(charTemp) != -1) {
int size = charTemp.length;
fos.write(charTemp, 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();
}
}

}
}