From e2416ddb8504bcc6b202b908639eb158b5a745fe Mon Sep 17 00:00:00 2001 From: Joel Dice Date: Sat, 11 Aug 2012 08:21:14 -0600 Subject: [PATCH] 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. --- classpath/java/io/File.java | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/classpath/java/io/File.java b/classpath/java/io/File.java index d8bfd474b4..187549ebfd 100644 --- a/classpath/java/io/File.java +++ b/classpath/java/io/File.java @@ -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; }