文件的重命名與移動(dòng)操作
有時(shí)候?yàn)榱藢?duì)文件進(jìn)行統(tǒng)一訪問(wèn)與管理,需要把文件進(jìn)行重命名,并移動(dòng)到新的文件夾,如何實(shí)現(xiàn)呢?
一枚簡(jiǎn)單的java小程序即可實(shí)現(xiàn):
part_1:需求:我需要把<(e:\baiduyun\傳智播客_張孝祥_(kāi)java多線程與并發(fā)庫(kù)高級(jí)應(yīng)用視頻教程下載)>文件夾下的所有子文件夾下的視頻文件重命名,并移動(dòng)到新的位置<(e:\baiduyun\張孝祥_(kāi)java多線程與并發(fā)庫(kù))>;
part_2:目錄結(jié)構(gòu)如下:
e:\baiduyun
e:\baiduyun\傳智播客_張孝祥_(kāi)java多線程與并發(fā)庫(kù)高級(jí)應(yīng)用視頻教程下載
e:\baiduyun\傳智播客張孝祥_(kāi)java多線程與并發(fā)庫(kù)高級(jí)應(yīng)用視頻教程下載\01傳智播客張孝祥傳統(tǒng)線程技術(shù)回顧
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 該程序?qū)崿F(xiàn)文件的重命名與移動(dòng)操作; */ public class renamefiles { private static file srcfolder; // 源文件夾 private static file destfolder; // 目的文件夾 private static list<file> srcfiles; // 存放所有待命名的文件 static { srcfolder = new file( "e:\\baiduyun\\傳智播客_張孝祥_(kāi)java多線程與并發(fā)庫(kù)高級(jí)應(yīng)用視頻教程下載" ); destfolder = new file( "e:\\baiduyun\\張孝祥_(kāi)java多線程與并發(fā)庫(kù)" ); srcfiles = new arraylist<file>(); } public static void main(string[] args) { // 對(duì)文件夾的合法性(是否存在)進(jìn)行校驗(yàn) try { checkfolder(); } catch (filenotfoundexception e) { e.printstacktrace(); return ; } // 遍歷源文件夾把要修改的文件放到集合中 iterategetfiles(renamefiles.srcfolder); // 對(duì)集合中的元素進(jìn)行重命名(并移動(dòng)到目標(biāo)文件夾) iteraterename(); } // end method-main private static void checkfolder() throws filenotfoundexception { if (!renamefiles.srcfolder.exists()) { throw new filenotfoundexception( "指定的源文件夾不存在." ); } if (!renamefiles.destfolder.exists()) { throw new filenotfoundexception( "指定的目標(biāo)文件夾不存在." ); } } private static void iteraterename() { string aviname = null ; string tempstr = null ; stringbuilder strbuilder = new stringbuilder(); file tempfile = null ; string sequencenumber = null ; string detailname = null ; // 遍歷list集合,逐個(gè)進(jìn)行重命名 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)); // 兩位的序號(hào),不足兩位用0補(bǔ)齊,例如:01 detailname = tempstr.substring(tempstr.lastindexof( "_" ) + 1 ); // 視頻文件的詳細(xì)內(nèi)容,例如:傳統(tǒng)線程互斥技術(shù) strbuilder.append(sequencenumber + "_" + detailname + ".avi" ); tempfile = new file(renamefiles.destfolder, strbuilder.tostring()); // 新文件的path // each.renameto(tempfile);//核心代碼(實(shí)現(xiàn)重命名和移動(dòng)) system.out.println(tempfile.tostring()); // 打印到控制臺(tái)以便調(diào)試 strbuilder.delete( 0 , strbuilder.length()); // 切記將strbuilder進(jìn)行清空 } // end foreach } // end method-iteraterename private static void iterategetfiles(file srcfile) { // 如果是文件夾,就繼續(xù)深入遍歷 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:重命名及移動(dòng)后的效果:
e:\baiduyun\張孝祥_(kāi)java多線程與并發(fā)庫(kù)
總結(jié)
以上就是本文關(guān)于java文件的重命名與移動(dòng)操作實(shí)例代碼的全部?jī)?nèi)容,希望對(duì)大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對(duì)本站的支持!
原文鏈接:http://blog.csdn.net/love_legain/article/details/54972387