Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Parsing File "roots" - break this code (http://www.velocityreviews.com/forums/t149747-parsing-file-roots-break-this-code.html)

Ian Pilcher 01-28-2006 09:08 PM

Parsing File "roots" - break this code
 
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 i.pilcher@comcast.net
================================================== ======================


All times are GMT. The time now is 06:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57