Java 中 更改文件路徑

java 中更改文件路徑可以用file.renameTo 方法

public static void moveDataFile2Reject() throws Exception {

	try { 

		//String dataFilePath = PropertyUtil.getString(PropertyUtil.INPUT_FILE_PATH_KEY);

		//String rejectFilePath = PropertyUtil.getString(PropertyUtil.REJECT_FILE__PATH_KEY);

		String dataFilePath = "D:\\a\b";

		String rejectFilePath = "D:\\a\reject";

		File file = new File(dataFilePath);

		File[] files = file.listFiles();

		for (File f : files) {

			if(f.isFile())

			{

				f.renameTo(new File(rejectFilePath + f.getName()));

			} 

		}

	} catch (Exception e) {

		logger.error("moveInFile2Reject Failed" , e);

	}

}


 

//java api:

Renames the file denoted by this abstract pathname.

Many aspects of the behavior of this method are inherently platform-dependent: The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章