Rename file in java not working
Who else edits it? Who reads it? If the answer is other parts of your own Java program, then the solution will be to call the close method on all Readers, Writers, or whatever else is using the file. Use a finally blcok to ensure that close is called. You should always use such techniques anyway, so that you aren't stuck later trying to debug a problem like this.
If the answer is other programs or processes running on the same system, then it's harder, depending on the situation. Find those other processes and kill them, if you can. Otherwise you probably need to reconsider your process and program design, so that your program can work with the other processes, rather than fighting with them.
Men, Thank you for your replies. I have got that problem solved. I double checked my code, that sometimes the file1 does not exist so the renameTo returns false. The problem is that f. You then massage this String and turn it back into a File. But the File now represents a path relative to the current directory, not the directory containing the original path.
As a result your code is actually attempting to rename the files from dirName into the application's current directory. That could fail because files already exist in the current directory with those names, or because the dirName and the current directory are in different file systems. You cannot rename a file from one filesystem to another Please note that a File in Java represents a pathname, not a file or a folder. In your code, the f objects are the pathnames for file system objects either files or folders in the directory denoted by the String dirname.
Each of these f objects will have a directory part. So my theory is that the same think is happening when the File. For some reason that is no clear to me, Java could be using the wrong encoding, and as a result producing a "name" for the original file that doesn't match the name of the file in the file system.
That would be sufficient to cause the rename to fail. So subbedName becomes a relative path file. Try something with f. How are we doing? Toggle navigation. JDK : File. Type: Bug Component: core-libs Sub-Component: java.
Submitted: Updated: Resolved: JDK 1. I did a similar thing, but with a ms sleep in the loop. Cheers Kactus. Kactus Kactus 7 7 bronze badges. Hmm, this code deletes srcFile even if renameTo or destFile. Jonik, Thanx, fixed code to not delete src file if renaming fails. Thanks for sharing this fixed my rename issue on windows. Johnydep Johnydep 5, 19 19 gold badges 52 52 silver badges 72 72 bronze badges. Super, this is much better! Why not Native; import com. Tharaka Tharaka 2, 1 1 gold badge 19 19 silver badges 12 12 bronze badges.
Marcus Becker Marcus Becker 4 4 silver badges 11 11 bronze badges. SomeBody 5, 2 2 gold badges 13 13 silver badges 29 29 bronze badges. BlueWhale BlueWhale 11 1 1 bronze badge. I would in any case add some timeout, maybe also give other threads the chance to do something also a wait or something, to lower the CPU stress would be helpful — Argeman.
Ravi Wallau Ravi Wallau 10k 2 2 gold badges 23 23 silver badges 34 34 bronze badges. Anton Gogolev Anton Gogolev k 37 37 gold badges silver badges bronze badges. But since robocopy is not a Java library, it probably wouldn't be very easy to bundle it and use it from my Java code Blindy Blindy I feel going through the trouble of wrapping this in JNI is greater than the effort required to wrap up robocopy in a Process decorator. Thanks, I might consider this if it doesn't get too complicated.
I haven't ever used JNI, and couldn't find good examples of calling a Windows kernel function on SO, so I posted this question: stackoverflow.
You could try a generic JNI wrapper like johannburkard. I have tested on windows 7 and works perfectly fine. There are cases where renameTo does not work reliably; that's the whole point of the question. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password.
0コメント