strip trailing separators when normalizing java.io.File.path

This addresses the case of e.g. new File("c:/foo/").exists() returning
false when the specified directory really does exist.
This commit is contained in:
Joel Dice 2012-08-11 08:21:14 -06:00
parent 2642a167e2
commit e2416ddb85

View File

@ -72,12 +72,16 @@ public class File implements Serializable {
}
}
private static String normalize(String path) {
if ("\\".equals(FileSeparator)) {
return path.replace('/', '\\');
} else {
return path;
private static String stripSeparators(String p) {
while (p.endsWith(FileSeparator)) {
p = p.substring(0, p.length() - 1);
}
return p;
}
private static String normalize(String path) {
return stripSeparators
("\\".equals(FileSeparator) ? path.replace('/', '\\') : path);
}
public static native boolean rename(String old, String new_);
@ -148,13 +152,9 @@ public class File implements Serializable {
}
public String getParent() {
String p = path;
while (p.endsWith(FileSeparator)) {
p = p.substring(0, p.length() - 1);
}
int index = p.lastIndexOf(FileSeparator);
int index = path.lastIndexOf(FileSeparator);
if (index >= 0) {
return p.substring(0, index);
return normalize(path.substring(0, index));
} else {
return null;
}