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