Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Parsing File "roots" - break this code

Reply
Thread Tools

Parsing File "roots" - break this code

 
 
Ian Pilcher
Guest
Posts: n/a
 
      01-28-2006
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 http://www.velocityreviews.com/forums/(E-Mail Removed)
================================================== ======================
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
`if (!p ? i++ : 0) break;' == `if (!p){ i++; break;}' ? lovecreatesbea...@gmail.com C Programming 12 04-14-2008 07:59 AM
[ANN] Parsing Tutorial and YARD 1.0: A C++ Parsing Framework Christopher Diggins C++ 0 07-09-2007 09:01 PM
In file parsing, taking the first few characters of a text file after a readfile or streamreader file read... .Net Sports ASP .Net 11 01-17-2006 12:44 AM
SAX Parsing - Weird results when parsing content between tags. Naren XML 0 05-11-2004 07:25 PM
Perl expression for parsing CSV (ignoring parsing commas when in double quotes) GIMME Perl 2 02-11-2004 05:40 PM



Advertisments