文件的重命名與移動操作
有時候為了對文件進行統一訪問與管理,需要把文件進行重命名,并移動到新的文件夾,如何實現呢?
一枚簡單的java小程序即可實現:
part_1:需求:我需要把<(e:\baiduyun\傳智播客_張孝祥_java多線程與并發庫高級應用視頻教程下載)>文件夾下的所有子文件夾下的視頻文件重命名,并移動到新的位置<(e:\baiduyun\張孝祥_java多線程與并發庫)>;
part_2:目錄結構如下:
e:\baiduyun
e:\baiduyun\傳智播客_張孝祥_java多線程與并發庫高級應用視頻教程下載
e:\baiduyun\傳智播客張孝祥_java多線程與并發庫高級應用視頻教程下載\01傳智播客張孝祥傳統線程技術回顧
part_3:程序源碼+注釋:
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
package cn.mike.demo; import java.io.file; import java.io.filenotfoundexception; import java.util.arraylist; import java.util.list; /** * @author administrator * @usage 該程序實現文件的重命名與移動操作; */ public class renamefiles { private static file srcfolder; // 源文件夾 private static file destfolder; // 目的文件夾 private static list<file> srcfiles; // 存放所有待命名的文件 static { srcfolder = new file( "e:\\baiduyun\\傳智播客_張孝祥_java多線程與并發庫高級應用視頻教程下載" ); destfolder = new file( "e:\\baiduyun\\張孝祥_java多線程與并發庫" ); srcfiles = new arraylist<file>(); } public static void main(string[] args) { // 對文件夾的合法性(是否存在)進行校驗 try { checkfolder(); } catch (filenotfoundexception e) { e.printstacktrace(); return ; } // 遍歷源文件夾把要修改的文件放到集合中 iterategetfiles(renamefiles.srcfolder); // 對集合中的元素進行重命名(并移動到目標文件夾) iteraterename(); } // end method-main private static void checkfolder() throws filenotfoundexception { if (!renamefiles.srcfolder.exists()) { throw new filenotfoundexception( "指定的源文件夾不存在." ); } if (!renamefiles.destfolder.exists()) { throw new filenotfoundexception( "指定的目標文件夾不存在." ); } } private static void iteraterename() { string aviname = null ; string tempstr = null ; stringbuilder strbuilder = new stringbuilder(); file tempfile = null ; string sequencenumber = null ; string detailname = null ; // 遍歷list集合,逐個進行重命名 for (file each : renamefiles.srcfiles) { aviname = each.getname().substring( 0 , each.getname().length() - 4 ); // 獲取文件名稱(除去后綴名".avi") tempstr = each.getparent(); // 父文件夾的名稱 sequencenumber = string.format( "%02d" , integer.valueof(aviname)); // 兩位的序號,不足兩位用0補齊,例如:01 detailname = tempstr.substring(tempstr.lastindexof( "_" ) + 1 ); // 視頻文件的詳細內容,例如:傳統線程互斥技術 strbuilder.append(sequencenumber + "_" + detailname + ".avi" ); tempfile = new file(renamefiles.destfolder, strbuilder.tostring()); // 新文件的path // each.renameto(tempfile);//核心代碼(實現重命名和移動) system.out.println(tempfile.tostring()); // 打印到控制臺以便調試 strbuilder.delete( 0 , strbuilder.length()); // 切記將strbuilder進行清空 } // end foreach } // end method-iteraterename private static void iterategetfiles(file srcfile) { // 如果是文件夾,就繼續深入遍歷 if (srcfile.isdirectory()) { file[] files = srcfile.listfiles(); for (file each : files) { iterategetfiles(each); } } else if (srcfile.getabsolutepath().endswith( ".avi" )) { // 不是文件夾而且文件格式為avi,就將該文件添加到待命名文件的list集合中 renamefiles.srcfiles.add(srcfile); } } // end method-iterategetfiles } // end class-renamefiles |
part_4:重命名及移動后的效果:
e:\baiduyun\張孝祥_java多線程與并發庫
總結
以上就是本文關于java文件的重命名與移動操作實例代碼的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/love_legain/article/details/54972387