轉自:http://blog.csdn.net/wlwqw/article/details/2037930【帶代碼進行了整理】
起因:上份工作辭掉了,找新工作,筆試題有一道題按照所給出的格式將重新命名文件名。
當時對io包中的file類操作生疏了,回來后整理了一下。
先簡單說下java.io.File的構造方法和常用方法,具體可參考JDK1.6文檔。
在線文檔:http://download.oracle.com/technetwork/java/javase/6/docs/zh/api/java/io/File.html
構造方法如下:
File(File parent, String child) 根據 parent 抽象路徑名和 child 路徑名字符串創建一個新 File 實例。 |
File(String pathname) 通過將給定路徑名字符串轉換為抽象路徑名來創建一個新 File 實例。 |
File(String parent, String child) 根據 parent 路徑名字符串和 child 路徑名字符串創建一個新 File 實例。 |
File(URI uri) 通過將給定的 file: URI 轉換為一個抽象路徑名來創建一個新的 File 實例。 |
代碼如下:
1 package com.file; 2 import java.io.File; 5
6 public class ChangeFileName { 7 public static void main(String args[]) { 8
9 File fl = new File("d://文件夾"); // 這里寫上發替換的文件夾路徑,注意使用雙斜杠
10 String[] files = fl.list(); 11 File f = null; 12 String filename = ""; 13
14 for(String file:files){ 15 /*
16 * 注意,這里一定要寫成File(fl,file) 17 * 如果寫成File(file)是行不通的, 18 * 一定要全路徑 19 */
20 f = new File(fl,file); 21 filename = f.getName(); 22 //System.out.println(filename);
23 /*
24 * 這里可以反復使用replace替換, 25 * 當然也可以使用正則表達式來替換了 26 */
27 f.renameTo(new File(fl.getAbsolutePath()+"//"+filename.replace("要替換掉的內容", "替換成的內容"))); 28 } 29 } 30 }
另外講述下java.io.File的一些常見用法。
1 import java.io.*; 2
3 public class FileOperate { 4 public FileOperate() { 5 } 6
7 /**
8 * 新建目錄 9 * @param folderPath String 如 c:/fqf 10 * @return boolean 11 */
12 public void newFolder(String folderPath) { 13 try { 14 String filePath = folderPath; 15 filePath = filePath.toString(); 16 java.io.File myFilePath = new java.io.File(filePath); 17 if (!myFilePath.exists()) { 18 myFilePath.mkdir(); 19 } 20 } 21 catch (Exception e) { 22 System.out.println("新建目錄操作出錯"); 23 e.printStackTrace(); 24 } 25 } 26
27 /**
28 * 新建文件 29 * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt 30 * @param fileContent String 文件內容 31 * @return boolean 32 */
33 public void newFile(String filePathAndName, String fileContent) { 34
35 try { 36 String filePath = filePathAndName; 37 filePath = filePath.toString(); 38 File myFilePath = new File(filePath); 39 if (!myFilePath.exists()) { 40 myFilePath.createNewFile(); 41 } 42 FileWriter resultFile = new FileWriter(myFilePath); 43 PrintWriter myFile = new PrintWriter(resultFile); 44 String strContent = fileContent; 45 myFile.println(strContent); 46 resultFile.close(); 47
48 } 49 catch (Exception e) { 50 System.out.println("新建目錄操作出錯"); 51 e.printStackTrace(); 52
53 } 54
55 } 56
57 /**
58 * 刪除文件 59 * @param filePathAndName String 文件路徑及名稱 如c:/fqf.txt 60 * @param fileContent String 61 * @return boolean 62 */
63 public void delFile(String filePathAndName) { 64 try { 65 String filePath = filePathAndName; 66 filePath = filePath.toString(); 67 java.io.File myDelFile = new java.io.File(filePath); 68 myDelFile.delete(); 69
70 } 71 catch (Exception e) { 72 System.out.println("刪除文件操作出錯"); 73 e.printStackTrace(); 74
75 } 76
77 } 78
79 /**
80 * 刪除文件夾 81 * @param filePathAndName String 文件夾路徑及名稱 如c:/fqf 82 * @param fileContent String 83 * @return boolean 84 */
85 public void delFolder(String folderPath) { 86 try { 87 delAllFile(folderPath); //刪除完里面所有內容
88 String filePath = folderPath; 89 filePath = filePath.toString(); 90 java.io.File myFilePath = new java.io.File(filePath); 91 myFilePath.delete(); //刪除空文件夾
92
93 } 94 catch (Exception e) { 95 System.out.println("刪除文件夾操作出錯"); 96 e.printStackTrace(); 97
98 } 99
100 } 101
102 /**
103 * 刪除文件夾里面的所有文件 104 * @param path String 文件夾路徑 如 c:/fqf 105 */
106 public void delAllFile(String path) { 107 File file = new File(path); 108 if (!file.exists()) { 109 return; 110 } 111 if (!file.isDirectory()) { 112 return; 113 } 114 String[] tempList = file.list(); 115 File temp = null; 116 for (int i = 0; i < tempList.length; i++) { 117 if (path.endsWith(File.separator)) { 118 temp = new File(path + tempList[i]); 119 } 120 else { 121 temp = new File(path + File.separator + tempList[i]); 122 } 123 if (temp.isFile()) { 124 temp.delete(); 125 } 126 if (temp.isDirectory()) { 127 delAllFile(path+"/"+ tempList[i]);//先刪除文件夾里面的文件
128 delFolder(path+"/"+ tempList[i]);//再刪除空文件夾
129 } 130 } 131 } 132
133 /**
134 * 復制單個文件 135 * @param oldPath String 原文件路徑 如:c:/fqf.txt 136 * @param newPath String 復制后路徑 如:f:/fqf.txt 137 * @return boolean 138 */
139 public void copyFile(String oldPath, String newPath) { 140 try { 141 int bytesum = 0; 142 int byteread = 0; 143 File oldfile = new File(oldPath); 144 if (oldfile.exists()) { //文件存在時
145 InputStream inStream = new FileInputStream(oldPath); //讀入原文件
146 FileOutputStream fs = new FileOutputStream(newPath); 147 byte[] buffer = new byte[1444]; 148 int length; 149 while ( (byteread = inStream.read(buffer)) != -1) { 150 bytesum += byteread; //字節數 文件大小
151 System.out.println(bytesum); 152 fs.write(buffer, 0, byteread); 153 } 154 inStream.close(); 155 } 156 } 157 catch (Exception e) { 158 System.out.println("復制單個文件操作出錯"); 159 e.printStackTrace(); 160
161 } 162
163 } 164
165 /**
166 * 復制整個文件夾內容 167 * @param oldPath String 原文件路徑 如:c:/fqf 168 * @param newPath String 復制后路徑 如:f:/fqf/ff 169 * @return boolean 170 */
171 public void copyFolder(String oldPath, String newPath) { 172
173 try { 174 (new File(newPath)).mkdirs(); //如果文件夾不存在 則建立新文件夾
175 File a=new File(oldPath); 176 String[] file=a.list(); 177 File temp=null; 178 for (int i = 0; i < file.length; i++) { 179 if(oldPath.endsWith(File.separator)){ 180 temp=new File(oldPath+file[i]); 181 } 182 else{ 183 temp=new File(oldPath+File.separator+file[i]); 184 } 185
186 if(temp.isFile()){ 187 FileInputStream input = new FileInputStream(temp); 188 FileOutputStream output = new FileOutputStream(newPath + "/" +
189 (temp.getName()).toString()); 190 byte[] b = new byte[1024 * 5]; 191 int len; 192 while ( (len = input.read(b)) != -1) { 193 output.write(b, 0, len); 194 } 195 output.flush(); 196 output.close(); 197 input.close(); 198 } 199 if(temp.isDirectory()){//如果是子文件夾
200 copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 201 } 202 } 203 } 204 catch (Exception e) { 205 System.out.println("復制整個文件夾內容操作出錯"); 206 e.printStackTrace(); 207
208 } 209
210 } 211
212 /**
213 * 移動文件到指定目錄 214 * @param oldPath String 如:c:/fqf.txt 215 * @param newPath String 如:d:/fqf.txt 216 */
217 public void moveFile(String oldPath, String newPath) { 218 copyFile(oldPath, newPath); 219 delFile(oldPath); 220
221 } 222
223 /**
224 * 移動文件到指定目錄 225 * @param oldPath String 如:c:/fqf.txt 226 * @param newPath String 如:d:/fqf.txt 227 */
228 public void moveFolder(String oldPath, String newPath) { 229 copyFolder(oldPath, newPath); 230 delFolder(oldPath); 231
232 } 233 }