I
Ian Pilcher
I am trying to develop a more useful representation of abstract
pathnames than that provided by java.io.File. One of the things it does
is differentiate directories from regular files. So, for example:
Path dirPath = Path.fromDirectory("/foo/bar/baz");
Path filePath = Path.fromRegularFile("/foo/bar/baz");
dirPath and filePath are not equal, and their toString methods return:
dirPath.toString(): /foo/bar/baz/
filePath.toString(): /foo/bar/baz
I want to provide a factory method fromPathname(String), which will
create the appropriate type of Path. I originally planned to simply
test pathName.endsWith(File.separator), but that won't work reliably on
Windows, where both \ and / are recognized as directory separators, but
File.separator only contains the backslash.
Here is the algorithm that I've come up with:
private static boolean namesRegularFile(String pathName)
{
File pathFile = new File(pathName);
String name = pathFile.getName();
if (name.length() == 0)
return false; // empty path or prefix only
return pathName.endsWith(name);
}
Anyone see any problems with this approach?
(BTW, anyone else think that the API provided by java.io.File is
glaringly inadequate?)
pathnames than that provided by java.io.File. One of the things it does
is differentiate directories from regular files. So, for example:
Path dirPath = Path.fromDirectory("/foo/bar/baz");
Path filePath = Path.fromRegularFile("/foo/bar/baz");
dirPath and filePath are not equal, and their toString methods return:
dirPath.toString(): /foo/bar/baz/
filePath.toString(): /foo/bar/baz
I want to provide a factory method fromPathname(String), which will
create the appropriate type of Path. I originally planned to simply
test pathName.endsWith(File.separator), but that won't work reliably on
Windows, where both \ and / are recognized as directory separators, but
File.separator only contains the backslash.
Here is the algorithm that I've come up with:
private static boolean namesRegularFile(String pathName)
{
File pathFile = new File(pathName);
String name = pathFile.getName();
if (name.length() == 0)
return false; // empty path or prefix only
return pathName.endsWith(name);
}
Anyone see any problems with this approach?
(BTW, anyone else think that the API provided by java.io.File is
glaringly inadequate?)