在java中,我們對(duì)文件的操作,包含了很多方式,下面就是一個(gè)移動(dòng)文件的方法,積累下來(lái),做個(gè)記錄
1 public static boolean moveFileForBack(String srcFile, String destPath) {
2 try {
3 if (!(new File(destPath).isDirectory())) {
4 new File(destPath).mkdir();
5 // new File("D:/java/newdata/2003-04-14/").mkdir();
6 }
7 } catch (SecurityException e) {
8 System.out.println("can not make directory");
9 }
10 File file = new File(srcFile);
11 File dir = new File(destPath);
12 boolean success = file.renameTo(new File(dir, file.getName()));
13 return success;
14 }
還有一個(gè),就是移動(dòng)的位置,如果沒有文件夾,我們就創(chuàng)建一個(gè),是上面代碼的深入探究。
1 public static boolean moveFileForBack(String srcFile, String destPath) {
2 try {
3 if (!(new File(destPath).isDirectory())) {
4 new File(destPath).mkdir();
5 // new File("D:/java/newdata/2003-04-14/").mkdir();
6 }
7 } catch (SecurityException e) {
8 System.out.println("can not make directory");
9 }
10 File file = new File(srcFile);
11 File dir = new File(destPath);
12 boolean success = file.renameTo(new File(dir, file.getName()));
13 return success;
14 }
15