通过路径移动该目录下的所有文件 (renameTo用法)

package com.hdzx.util;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.WordUtils;

import com.trs.web2frame.domain.WAppendixMgr;
import com.trs.web2frame.domain.WDocumentMgr;
import com.trs.web2frame.entity.WAppendix;
import com.trs.web2frame.entity.WDocument;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		WDocument wDocument;
		try {
//			wDocument = WDocumentMgr.findBySId(30, 0, "1BE1940D90C757B54825786300326221");
//			System.out.println(wDocument.getFieldValue("DOCtitle"));
			moveFile("");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static String FILE_PATH="D:/yjs/2012113/";
	/**
	 * 移动文件
	 * @param inputXml
	 * @param filePath
	 */
	private static void moveFile(String paths) {
//		if(paths != null && paths.length() >0){
//			paths = paths.substring(0,paths.length()-1);
//			String[] fileStrs = paths.split(",");
//			for(String filePath : fileStrs){
				moveAllFile(FILE_PATH);
				File delFile = new File(FILE_PATH);
				if(delFile.exists())
					delFile.delete();
//			}
//		}
	}
	
	private static void moveAllFile(String path){
		File file = new File(path);
		if(!file.exists())
			return;
		String[] files = file.list();
		for(String str : files){
			File fl = new File(path+"/"+str);
			if(fl.isFile()){
				String toPath = FILE_PATH+"backup/"+path.replace(FILE_PATH, "")+"/";
				File newDir = new File(toPath);
				if(!newDir.exists())
					newDir.mkdirs();
				File newFile = new File(toPath+fl.getName());
				fl.renameTo(newFile);
			}else if(fl.isDirectory()){
				moveAllFile(fl.getAbsolutePath().replaceAll("\\\\", "/"));
			}
			fl.delete();
		}
	}

}

你可能感兴趣的:(name)