1. 程式人生 > >java 一些比較

java 一些比較

1.java file renameTo

	public static void main(String[] args) {
		File source = new File("D:/workSpace/Test_SM/Test_file/test.txt");
		File target = new File("D:/workSpace/Test_SM/Test_file/test2.txt");
//import org.apache.commons.io.FileUtils;
		try {
			FileUtils.moveFile(source, target);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
//import java.io.File;
		boolean flag = source.renameTo(target);
		if (flag) {
			System.out.println("File renamed successfully");
		} else {
			System.out.println("Rename operation failed");
		}
	}

測試點

a.目標檔案存在

org.apache.commons.io.FileExistsException: Destination 'D:\workSpace\Test_SM\Test_file\test2.txt' already exists     at org.apache.commons.io.FileUtils.moveFile(FileUtils.java:2568)     at com.testMain.Test.main(Test.java:15) Rename operation failed

b.原始檔不存在

java.io.FileNotFoundException: Source 'D:\workSpace\Test_SM\Test_file\test1.txt' does not exist     at org.apache.commons.io.FileUtils.moveFile(FileUtils.java:2562)     at com.testMain.Test.main(Test.java:15)

Rename operation failed