com.ccg.io
Class FileName

java.lang.Object
  extended by com.ccg.io.FileName

public class FileName
extends Object

Collection of static methods to manipulate file names.

This class houses a collection of static methods used to minipulate file names. You will find functions here which can be used to pick out file names, extensions, paths, etc. In addition, the methods are intended to be written such that they can be used by different OS environments (for example, both '/' and '\\' are recognized regardless of the platform you are running on).

Since:
1.0
Version:
$Revision: 1.1.1.1 $
Author:
$Author: pkb $
See Also:
Copy

Constructor Summary
FileName()
           
 
Method Summary
static String fixSlashes(String fname)
          This method changes '/' or '\' characters to JVM's native form.
static String getLastExtension(String fn, String def)
          Extract the file "extension" from a file name.
static String getNameAndExtension(String name)
          Get just the file name and extension portion of a file name.
static String removeLastExtension(String fn)
          Removes the last extension from a file name.
static String setLastExtension(String origName, String new_ext)
          Sets the last extension of the filename.
static String subtractBaseFromName(String fname, String base)
          Subtract off a leading "base path" from a full file path name.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FileName

public FileName()
Method Detail

getLastExtension

public static String getLastExtension(String fn,
                                      String def)
Extract the file "extension" from a file name.

In the Unix/Windows world, it is a common practice to end a file name with a "extension". The "extension" is a acronym used to identify the contents of the file. For example, Java source files typically have the extension: ".java".

This method will look for a "extension" at the end of a file and return it if possible. If an extension can't be determined from the filename, then the default value passed will be returned. Here are some examples if passed two arguments (fname,defExt) and the results (-> "results"):

 ("t.java",".j") -> ".java"
 ("t",".j") -> ".j"
 ("t.",".j") -> "."
 ("t.java.gz",".j") -> ".gz"
 ("t",null) -> null
 (null,".j") -> ".j"
 (null,null) -> null
 

It should be noted that if a filename contains multiple extensions, ONLY the last extension will be returned.

Parameters:
fname - The file name to search for the extension. If you pass null (or if the fname doesn't contain an extension), you'll get the default value back.
defExt - The default extension you would like returned if we are unable to determine one from the file name passed.
Returns:
The "extension" name of the file as described above, or the default value passed if no extension was found.
Since:
1.0

removeLastExtension

public static String removeLastExtension(String fn)
Removes the last extension from a file name.

In the Unix/Windows world, it is a common practice to end a file name with a "extension". The "extension" is a acronym used to identify the contents of the file. For example, Java source files typically have the extension: ".java".

This method will look for the last "extension" at the end of a file and remove it. If an extension isn't found, then the original value passed is returned. Here are some examples if passed two arguments (fname,defExt) and the results (-> "results"):

 ("t.java") -> "t"
 ("t") -> "t"
 (".t") -> ""
 ("t.java.gz") -> "t.java"
 ("t.dir/test") -> "t.dir/test"
 (null) -> null
 

It should be noted that if a filename contains multiple extensions, ONLY the last extension will be removed. Also, if the file name does not contain an extension, but one of the parent directories does, then the original value is returned (for example "t.dir/test" comes back as "t.dir/test").

Parameters:
fname - The file name to remove the extension from.
Returns:
The "extensionless" name of the file as described above, or the original value passed if no extension was found.
Since:
1.0

setLastExtension

public static String setLastExtension(String origName,
                                      String new_ext)
Sets the last extension of the filename.

This method is used to set the last extension of the filename. For example, if you wanted to change the name such that it ended with ".cab" instead of ".jar" you would pass ".cab" to this method. If the file name doesn't currently have an extension, the new extension passed will be added.

Parameters:
origName - The original name of the file (must not be null).
ext - New extension for the file name (must not be null).
Returns:
The new name of the file with the new extension
Since:
1.0

getNameAndExtension

public static String getNameAndExtension(String name)
Get just the file name and extension portion of a file name.

This method scans a string backwards looking for a directory separator (a '/', '\', ':' or separatorChar character). The first occurance results in the remaining portion of the string being returned. This code should work for all file names regardless of OS.

The following example program demonstrates how one might use this:

import com.ccg.io.*;

public class T {

  static public void appendString(StringBuffer to, String s) {
    if (s == null) to.append("null");
    else {
      to.append('\"');
      to.append(s);
      to.append('\"');
    }
  }

  public static void showNameAndExtension(String fname) {
    StringBuffer line = new StringBuffer("FileName.getNameAndExtension(");
    appendString(line,fname);
    line.append(")=");
    appendString(line,FileName.getNameAndExtension(fname));

    System.out.println(line.toString());
  }

  static public void main(String args[]) {
    showNameAndExtension(null);
    showNameAndExtension("");
    showNameAndExtension("fred/java.txt");
    showNameAndExtension("/fred/java.txt");
    showNameAndExtension("e:\\fred\\java.txt");
    showNameAndExtension("\\fred\\java.txt");
    showNameAndExtension("fred\\java.txt");
    showNameAndExtension("java.txt");
    showNameAndExtension("d:java.txt");
  }
}
 

Running the above program should produce the following output:

FileName.getNameAndExtension(null)=null
FileName.getNameAndExtension("")=""
FileName.getNameAndExtension("fred/java.txt")="java.txt"
FileName.getNameAndExtension("/fred/java.txt")="java.txt"
FileName.getNameAndExtension("e:\fred\java.txt")="java.txt"
FileName.getNameAndExtension("\fred\java.txt")="java.txt"
FileName.getNameAndExtension("fred\java.txt")="java.txt"
FileName.getNameAndExtension("java.txt")="java.txt"
FileName.getNameAndExtension("d:java.txt")="java.txt"
 

Parameters:
name - Original name of file to remove path information from.
Returns:
All leading path information removed (or null if you pass null).
Since:
1.0

subtractBaseFromName

public static String subtractBaseFromName(String fname,
                                          String base)
Subtract off a leading "base path" from a full file path name.

This method attempts to "smartly" remove a leading set of directories from an file name. For example, if you had a filename like: "/a/long/path/fname.txt", using this method, you could subtract off the base of "/a/long" producing the relative name of "path/fname.txt".

Here are some highlights of this method:

The following program gives a good idea of how one can use this method:

import com.ccg.io.*;

public class T {

  static public void appendString(StringBuffer to, String s) {
    if (s == null) to.append("null");
    else {
      to.append('\"');
      to.append(s);
      to.append('\"');
    }
  }

  static public void showResults(String fname, String base) {
    StringBuffer line = new StringBuffer("FileName.subtractBaseFromName(");
    appendString(line,fname);
    line.append(',');
    appendString(line,base);
    line.append(")=");
    appendString(line,FileName.subtractBaseFromName(fname,base));

    System.out.println(line.toString());
  }


  static public void main(String args[]) {

    showResults(null,null);
    showResults(null,"/tmp");
    showResults("c:\\home\\joe.txt",null);
    showResults("/home/joe/t.txt","/tmp");
    showResults("t.txt","/tmp");
    showResults("\\tmp\\t.txt","\\tmp");
    showResults("\\tmp\\t.txt","/tmp/");
    showResults("/long/long/path/t.txt","\\long\\long");
    showResults("/long/long","\\long\\long");
    showResults("d:/long/long/","d:\\long\\long");
    showResults("/long/long","\\long\\long\\");
  }
}
 

Running the above should produce the following output:

FileName.subtractBaseFromName(null,null)=null
FileName.subtractBaseFromName(null,"/tmp")=null
FileName.subtractBaseFromName("c:\home\joe.txt",null)="c:\home\joe.txt"
FileName.subtractBaseFromName("/home/joe/t.txt","/tmp")="/home/joe/t.txt"
FileName.subtractBaseFromName("t.txt","/tmp")="t.txt"
FileName.subtractBaseFromName("\tmp\t.txt","\tmp")="t.txt"
FileName.subtractBaseFromName("\tmp\t.txt","/tmp/")="t.txt"
FileName.subtractBaseFromName("/long/long/path/t.txt","\long\long")="path/t.txt"
FileName.subtractBaseFromName("/long/long","\long\long")=""
FileName.subtractBaseFromName("d:/long/long/","d:\long\long")=""
FileName.subtractBaseFromName("/long/long","\long\long\")=""
 

Parameters:
fname - The filename you want to strip of some leading path information. You can pass null - you'll get null back.
base - The base directory you want to remove from the filename (if you pass null, you get the original file name back).
Returns:
Returns the result of subtracting the "base" from the "fname" (only returns null if you pass a null "fname")
Since:
1.0

fixSlashes

public static String fixSlashes(String fname)
This method changes '/' or '\' characters to JVM's native form.

This method attempts to "correct" a string so that the path separator is changed to the JVM platforms native form. For example, if one passed this method a string like:

 "etc/mydir\myfile.txt"
 

This method would return "etc/mydir/myfile.txt" on platforms (such as Unix) that use a forward slash for directory separator. On a windows type of platform, the string "etc\mydir\myfile.txt" would be returned.

Parameters:
fname - File name you would like to make sure has the proper "slashes" (if you pass null you get null).
Returns:
File name that has been properly "slashified" for your JVM's OS environment.
Since:
1.0


Copyright 1998-1998-2006 null. All Rights Reserved.