Parsing File "roots" - break this code

Discussion in 'Java' started by Ian Pilcher, Jan 28, 2006.

  1. Ian Pilcher

    Ian Pilcher Guest

    The java.io.File class doesn't provide a lot of help when parsing file
    paths. In particular, it's difficult to determine whether a Windows
    path starts with a filesystem identifier (a drive letter), a root
    directory (a backslash), or both (c:\, \\, etc.).

    Below is a quick program to test the algorithm that I've developed.
    (Appologies in advance for any word wrap problems.) From what I can
    tell it works as expected on Windows XP and Linux. I'd appreciate any
    thoughts or testing, particularly if there's a Java platform that uses
    file path semantics different from those of UNIX and Windows.

    Note: In this scheme, root directories don't have names, nor does the
    UNIX filesystem root. Windows filesystem roots are either a drive
    letter followed by a colon (c: for example) or a single backslash.

    Thanks!

    import java.io.*;

    class Foo
    {
    public static void main(String[] args)
    {
    File dirFile = new File(args[0]);

    if (dirFile.getName().length() > 0)
    {
    processNormalDirectory(dirFile);
    }
    else
    {
    assert dirFile.getParent() == null;

    if (dirFile.isAbsolute())
    {
    processRootAndDir(dirFile);
    }
    else
    {
    if (dirFile.getPath().endsWith(File.separator))
    processRootDir(dirFile);
    else
    processFsRoot(dirFile);
    }
    }
    }

    static void processNormalDirectory(File dirFile)
    {
    System.out.println(dirFile.getPath() +
    " represents a normal directory named " +
    dirFile.getName());
    }

    static void processRootDir(File dirFile)
    {
    System.out.println(dirFile.getPath() + " represents a root
    directory");
    }

    static void processFsRoot(File dirFile)
    {
    System.out.println(dirFile.getPath() +
    " represents a filesystem root named " + dirFile.getPath());
    }

    static void processRootAndDir(File dirFile)
    {
    String rootName = dirFile.getPath();
    rootName = rootName.substring(0,
    rootName.length() - File.separator.length());

    System.out.println(dirFile.getPath() +
    " represents a filesystem root named " + rootName +
    " and a root directory");
    }
    }
    --
    ========================================================================
    Ian Pilcher
    ========================================================================
     
    Ian Pilcher, Jan 28, 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. Kevin Spencer

    Re: Application Roots for assemblies

    Kevin Spencer, Jul 3, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    442
    Ian Turner
    Jul 4, 2003
  2. Carl Johansen

    Single vs multiple IIS application roots

    Carl Johansen, Jun 7, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    3,386
    Shimon Sim
    Jun 8, 2005
  3. =?ISO-8859-1?Q?=22Einar_W=2E_H=F8st=22?=

    Newbie: List file system roots

    =?ISO-8859-1?Q?=22Einar_W=2E_H=F8st=22?=, Aug 23, 2007, in forum: Python
    Replies:
    3
    Views:
    281
    Bjoern Schliessmann
    Aug 24, 2007
  4. Replies:
    12
    Views:
    970
  5. jzakiya

    Roots Module (code)

    jzakiya, Jun 3, 2010, in forum: Ruby
    Replies:
    0
    Views:
    129
    jzakiya
    Jun 3, 2010
Loading...

Share This Page