Identifying alternate directory separators

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?)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top