renameTo()를 사용하여 한 디렉토리에서 다른 디렉토리로 파일을 이동하려고합니다. 그러나 renameTo는 작동하지 않습니다 (파일의 이름을 바꾸거나 이동하지 않음). 기본적으로 동일한 파일 이름을 가진 파일을 먼저 삭제 한 다음 anoter 디렉토리의 파일을 원래 파일을 삭제 한 동일한 위치로 복사 한 다음 동일한 이름으로 새 파일을 복사합니다.파일을 삭제하고 java 디렉토리의 파일로 이동
//filePath = location of original file with file name appended. ex: C:\Dir\file.txt
//tempPath = Location of file that I want to replace it to file file without the file name. ex: C:\AnotherDir
int pos = filePath.indexOf("C:\\Dir\\file.txt");
//Parse out only the path, so just C:\\Dir
String newFilePath = filePath.substring(0,pos-1);
//I want to delete the original file
File deletefile = new File(newFilePath,"file.txt");
if (deletefile.exists()) {
success = deletefile.delete();
}
//There is file already exists in the directory, but I am just appending .tmp at the end
File newFile = new File(tempPath + "file.txt" + ".tmp");
//Create original file again with same name.
File oldFile = new File(newFilePath, "file.txt");
success = oldFile.renameTo(newFile); // This doesnt work.
내가 잘못하고있는 것을 말해 줄 수 있습니까?
도움 주셔서 감사합니다.
무엇이 작동하지 않습니까? 어떤 종류의 오류 메시지가 나옵니까? 프로그램이 정상적으로 종료합니까? –