On Windows, certain filenames (which are technically not correctly constructed) will work with some calls, but not others. Here's some example code:
import java.io.*;
public class FSTest {
public static void main(String[] args) {
File testDir1 = new File("/\\c:\\WINDOWS\\TEMP\\A");
System.out.println(testDir1);
System.out.println(testDir1.mkdir());
System.out.println(testDir1.delete());
}
This code will create "A" in the temp folder, but delete will return false. File.Rename() will not work on "A" either (there may be other calls too). It's all because of the "\" in the front of the name. Normally, the workaround would be not to put the "\" in front, but URL.getFile prepends a "\" for some reason (see related bug), so getting a filename like this is more common than you might think.
import java.io.*;
public class FSTest {
public static void main(String[] args) {
File testDir1 = new File("/\\c:\\WINDOWS\\TEMP\\A");
System.out.println(testDir1);
System.out.println(testDir1.mkdir());
System.out.println(testDir1.delete());
}
This code will create "A" in the temp folder, but delete will return false. File.Rename() will not work on "A" either (there may be other calls too). It's all because of the "\" in the front of the name. Normally, the workaround would be not to put the "\" in front, but URL.getFile prepends a "\" for some reason (see related bug), so getting a filename like this is more common than you might think.
- duplicates
-
JDK-4042592 java.io.File.renameTo should work with non-canonical pathnames
- Closed
- relates to
-
JDK-4127567 URL.getFile prepend unnecessary "/" to file name
- Closed