Identifying alternate directory separators

Discussion in 'Java' started by Ian Pilcher, Feb 10, 2006.

  1. Ian Pilcher

    Ian Pilcher Guest

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

    --
    ========================================================================
    Ian Pilcher
    ========================================================================
    Ian Pilcher, Feb 10, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edward Grosso via .NET 247

    Save a Recordset to an ASCII file with custom field separators

    Edward Grosso via .NET 247, Jun 11, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    453
    David Browne
    Jun 11, 2004
  2. Ulf Meinhardt
    Replies:
    1
    Views:
    508
    J. David Boyd
    Mar 17, 2005
  3. dorayme

    inline list separators

    dorayme, Nov 6, 2006, in forum: HTML
    Replies:
    35
    Views:
    1,262
    BootNic
    Nov 9, 2006
  4. Chris
    Replies:
    2
    Views:
    665
    Chris
    Mar 4, 2007
  5. Replies:
    10
    Views:
    323
    Sean O'Halpin
    Jul 17, 2006
Loading...

Share This Page