|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.io.FileName
public class FileName
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).
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 |
|---|
public FileName()
| Method Detail |
|---|
public static String getLastExtension(String fn,
String def)
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.
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.
public static String removeLastExtension(String fn)
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").
fname - The file name to remove the extension from.
public static String setLastExtension(String origName,
String new_ext)
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.
origName - The original name of the file (must not be null).ext - New extension for the file name (must not be null).
public static String getNameAndExtension(String 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"
name - Original name of file to remove path information from.
public static String subtractBaseFromName(String fname,
String base)
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\")=""
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).
public static String fixSlashes(String fname)
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.
fname - File name you would like to make sure has the proper "slashes"
(if you pass null you get null).
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||