216 e.printStackTrace();
217
218 }
219 }
220
221 /**
222 * 文件夹的复制
223 *
224 * @param oldPath
225 * 原文件夹路径 如: c:\\hello
226 * @param newPath
227 * 新文件夹路径 如: e:\\hello
228 */
229 public void copyFolder(String oldPath, String newPath) {
230
231 try {
232 (new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹
233 File a = new File(oldPath);
234 String[] file = a.list();
235 File temp = null;
236 for (int i = 0; i < file.length; i++) {
237 if (oldPath.endsWith(File.separator)) {
238 temp = new File(oldPath + file[i]);
239 } else {
240 temp = new File(oldPath + File.separator + file[i]);
241 }
242
243 if (temp.isFile()) {
244 FileInputStream input = new FileInputStream(temp);
245 FileOutputStream output = new FileOutputStream(newPath
246 + "/" + (temp.getName()).toString());
247 byte[] b = new byte[1024 * 5];
248 int len;
249 while ((len = input.read(b)) != -1) {
250 output.write(b, 0, len);
251 }
252 output.flush();
253 output.close();
254 input.close();
255 }
256 if (temp.isDirectory()) {// 如果是子文件夹
257 copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);
258 }
259 }
260 } catch (Exception e) {
262 e.printStackTrace();
263
264 }
265 }
266
267 /**
268 * 移动单个文件
269 *
270 * @param oldPath
271 * 源文件路径 如:c:\\hello.java
272 * @param newPath
273 * 新文件路径 如:e:\\hello.java
274 */
275 public void moveFile(String oldPath, String newPath) {
276 copyFile(oldPath, newPath);
277 deleteFile(oldPath);
278 }
279
280 /**
281 * 移动文件夹
282 *
283 * @param oldPath
284 * 原文件夹路径 如:c:\\hongten
285 * @param newPath
286 * 新文件夹路径 如:e:\\hongten
287 */
288 public void moveFolder(String oldPath, String newPath) {
289 copyFolder(oldPath, newPath);
290 deleteFolder(oldPath);
291 }
292
293 /**
294 * 获得系统根目录绝对路径
295 *
296 * @return
297 */
298 public String getPath() {
299 String sysPath = this.getClass().getResource("/").getPath();
300 // 对路径进行修改
301 sysPath = sysPath.substring(1, sysPath.length() - 16);
302 return sysPath;
303 }
304
305 }
现在有时间把这些东西整理出来,给大家分享一下……
摘自 Hongten