1 /* -------------------------------------------------------------------
2 * Java source file for the class FilenameUtil
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:45 $
11 *
12 * $Id: FilenameUtil.java,v 1.1.1.1 2003/07/25 04:51:45 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.io;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.io.*;
27
28 /*====================================================================
29 Implementation of class FilenameUtil
30 ====================================================================*/
31 /***
32 * FilenameUtil defines several useful method to manipulate filename.
33 *
34 * @version $Revision: 1.1.1.1 $
35 * @author Masahiro Takatsuka (masa@jbeans.net)
36 */
37
38 public final class FilenameUtil {
39 /***
40 * Constructs a new FilenameUtil object.
41 */
42 private FilenameUtil() {
43 super();
44 }
45
46 /***
47 * The system-dependent default name-separator character. This field is
48 * initialized to contain the first character of the value of the system
49 * property <code>file.separator</code>. On UNIX systems the value of this
50 * field is <code>'/'</code>; on Win32 systems it is <code>'\'</code>.
51 *
52 * @see java.lang.System#getProperty(java.lang.String)
53 */
54 public static final char separatorChar = File.separatorChar;
55
56 /***
57 * The system-dependent default name-separator character, represented as a
58 * string for convenience. This string contains a single character, namely
59 * <code>{@link #separatorChar}</code>.
60 */
61 public static final String separator = File.separator;
62
63 /***
64 * The system-dependent path-separator character. This field is
65 * initialized to contain the first character of the value of the system
66 * property <code>path.separator</code>. This character is used to
67 * separate filenames in a sequence of files given as a <em>path list</em>.
68 * On UNIX systems, this character is <code>':'</code>; on Win32 systems it
69 * is <code>';'</code>.
70 *
71 * @see java.lang.System#getProperty(java.lang.String)
72 */
73 public static final char pathSeparatorChar = File.pathSeparatorChar;
74
75 /***
76 * The system-dependent path-separator character, represented as a string
77 * for convenience. This string contains a single character, namely
78 * <code>{@link #pathSeparatorChar}</code>.
79 */
80 public static final String pathSeparator = File.pathSeparator;
81
82 /***
83 * Returns the name of the file or directory denoted by this abstract
84 * pathname. This is just the last name in the pathname's name
85 * sequence. If the pathname's name sequence is empty, then the empty
86 * string is returned.
87 *
88 * @return The name of the file or directory denoted by this abstract
89 * pathname, or the empty string if this pathname's name sequence
90 * is empty
91 */
92 public final static String getName(String path) {
93 return path.substring(path.lastIndexOf(separatorChar) + 1);
94 }
95
96 /***
97 * returns a suffix of the specified filename.
98 * <PRE>
99 * Example: String suffix = StringUtil.getSuffix(filename);
100 * </PRE>
101 *
102 * @param filename a filename.
103 * @return a suffix.
104 */
105 public final static String getSuffix(String filename) {
106 return filename.substring(filename.lastIndexOf(".") + 1);
107 }
108
109 public final static String getExtension(String filename) {
110 return getSuffix(filename);
111 }
112
113 /***
114 * returns a filename without a suffix.
115 * <PRE>
116 * Example: String suffix = StringUtil.getBaseName(filename);
117 * </PRE>
118 *
119 * @param filename a filename.
120 * @return a base name.
121 */
122 public final static String getBaseName(String filename) {
123 return filename.substring(0, filename.indexOf("."));
124 }
125
126 public final static String changeSuffix(String filename, String suffix) {
127 int index = filename.lastIndexOf(".");
128 String result = filename;
129 if (index == -1) {
130 result = filename + "." + suffix;
131 } else {
132 result = filename.substring(0, index+1) + suffix;
133 }
134 return result;
135 }
136
137 public final static String setSuffix(String filename, String suffix) {
138 String sfx = (suffix.indexOf(".") == 0) ? suffix : ("." + suffix);
139 int index = filename.lastIndexOf(sfx);
140 String result = filename;
141
142 if (index == -1) { // suffix is not sfx!
143 result = filename + sfx;
144 }
145 return result;
146 }
147
148 public final static File setSuffix(File file, String suffix) {
149 String filename = setSuffix(file.getAbsolutePath(), suffix);
150 return new File(filename);
151 }
152 } // FilenameUtil
This page was automatically generated by Maven