001: /*
002: * Jacareto Copyright (c) 2002-2005
003: * Applied Computer Science Research Group, Darmstadt University of
004: * Technology, Institute of Mathematics & Computer Science,
005: * Ludwigsburg University of Education, and Computer Based
006: * Learning Research Group, Aachen University. All rights reserved.
007: *
008: * Jacareto is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation; either
011: * version 2 of the License, or (at your option) any later version.
012: *
013: * Jacareto is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
016: * General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public
019: * License along with Jacareto; if not, write to the Free
020: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
021: *
022: */
023:
024: package jacareto.toolkit.io;
025:
026: import java.io.File;
027:
028: import java.util.StringTokenizer;
029:
030: /**
031: * Some useful utilities for files.
032: *
033: * @author <a href="mailto:cspannagel@web.de">Christian Spannagel</a>
034: * @version 1.0
035: */
036: public class FileUtilities {
037: /**
038: * Creates a new instance.
039: */
040: public FileUtilities() {
041: }
042:
043: /**
044: * Returns the extension of a filename as a String.
045: *
046: * @param filename The name of the file
047: *
048: * @return The extension of the file (lower case), or the empty String ("") if the
049: * file does not have an extension
050: */
051: public static String getExtension(String filename) {
052: int lastDotPos = filename.lastIndexOf('.');
053:
054: if (lastDotPos != -1) {
055: return (filename.substring(lastDotPos + 1)).toLowerCase();
056: } else {
057: return "";
058: }
059: }
060:
061: /**
062: * Returns the extension of a file as a String.
063: *
064: * @param file The file
065: *
066: * @return The extension of the file (lower case), or the empty String ("") if the
067: * file does not have an extension
068: */
069: public static String getExtension(File file) {
070: return getExtension(file.getName());
071: }
072:
073: /**
074: * Returns the name of the file without extension. This method is complementary to {@link
075: * #getExtension(String)}
076: *
077: * @param filename the complete name of the file
078: *
079: * @return the filename without extension
080: */
081: public static String getNameWithoutExtension(String filename) {
082: String result = null;
083:
084: String extension = getExtension(filename);
085:
086: if ((extension != null) && !extension.equals("")) {
087: result = filename.substring(0, filename.length()
088: - extension.length() - 1);
089: } else {
090: result = filename;
091: }
092:
093: return result;
094: }
095:
096: /**
097: * Returns the name of the file without extension. This method is complementary to {@link
098: * #getExtension(File)}
099: *
100: * @param file the file
101: *
102: * @return the filename without extension
103: */
104: public static String getNameWithoutExtension(File file) {
105: return getNameWithoutExtension(file.getName());
106: }
107:
108: /**
109: * Returns all path entries contained in a single string. The string entries must be separated
110: * by a semicolon. Example: <code>c:\jdk1.3;c:\programs\;d:\test</code>
111: *
112: * @param paths path string
113: *
114: * @return the entries as array, or <code>null</code> if the given string is empty
115: */
116: public static String[] getEntries(String paths) {
117: try {
118: StringTokenizer tokenizer = new StringTokenizer(paths, ";",
119: false);
120: String[] result = null;
121:
122: if ((paths != null) && !paths.equals("")) {
123: result = new String[tokenizer.countTokens()];
124:
125: int i = 0;
126:
127: while (tokenizer.hasMoreTokens()) {
128: result[i++] = tokenizer.nextToken();
129: }
130: }
131:
132: return result;
133: } catch (NullPointerException n) {
134: return null;
135: }
136: }
137:
138: /**
139: * Deletes a whole directory and all files and directories contained in it.
140: *
141: * @param dir DOCUMENT ME!
142: */
143: public static void deleteDirectory(File dir) {
144: File[] content = dir.listFiles();
145:
146: for (int i = 0; i < content.length; i++) {
147: if (content[i].isDirectory()) {
148: deleteDirectory(content[i]);
149: } else {
150: content[i].delete();
151: }
152: }
153:
154: dir.delete();
155: }
156: }
|