Source code: javatools/io/FileUtils.java
1 /*
2 * FileUtils.java
3 *
4 * Created on 8 aprile 2002, 18.44
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.io;
26
27 import java.io.*;
28
29 /**
30 * Contains a set of function useful for files.
31 * @author Antonio Petrelli
32 * @version 0.0.1
33 */
34 public class FileUtils {
35
36 /** Creates new FileUtils */
37 public FileUtils() {
38 }
39
40 /** Returns the container directory for this file.
41 * @param inFile The file to be used.
42 * @return The container directory.
43 */
44 public static File getDirectory(File inFile) {
45 if (inFile.isFile())
46 return new File(getDirectory(inFile.getPath()));
47 else
48 return null;
49 }
50
51 /** Returns the containing directory of a file.
52 * @param inFilePath The path to the file.
53 * @return The container directory.
54 */
55 public static String getDirectory(String inFilePath) {
56 int pos;
57
58 pos = inFilePath.lastIndexOf(File.separatorChar);
59 return inFilePath.substring(0, pos + 1);
60 }
61
62 /** Returns the file name only from a file.
63 * @param inFile The file to be used.
64 * @return The file name.
65 */
66 public static String getFileName(File inFile) {
67 if (inFile.isFile())
68 return getFileName(inFile.getPath());
69 else
70 return null;
71 }
72
73 /** Returns the file name without the complete path.
74 * @param inFilePath The path to the file.
75 * @return The file name.
76 */
77 public static String getFileName(String inFilePath) {
78 int pos;
79
80 pos = inFilePath.lastIndexOf(File.separatorChar);
81 return inFilePath.substring(pos + 1);
82 }
83
84 /** Returns the extension (the one after the dot...) for a file.
85 * @param inFile The file to be used.
86 * @return The resulting extension (without dot).
87 */
88 public static String getExtension(File inFile) {
89 if (inFile.isFile())
90 return getExtension(inFile.getPath());
91 else
92 return null;
93 }
94
95 /** Returns the extension for a given path.
96 * @param inFilePath The file path.
97 * @return The resulting extension, without dot.
98 */
99 public static String getExtension(String inFilePath) {
100 int pos;
101 String tempString;
102
103 tempString = getFileName(inFilePath);
104 pos = tempString.lastIndexOf('.');
105 if (pos >= 0 && tempString.length() > pos)
106 return tempString.substring(pos + 1);
107 else
108 return null;
109 }
110
111 /** Returns the filename with no path and no extension.
112 * @param inFile The file to be used.
113 * @return The filename all alone.
114 */
115 public static String getFileNameWithNoExtension(File inFile) {
116 if (inFile.isFile())
117 return getFileNameWithNoExtension(inFile.getPath());
118 else
119 return null;
120 }
121
122 /** Returns the filename with no path and no extension.
123 * @param inFilePath The path to the file.
124 * @return The filename all alone.
125 */
126 public static String getFileNameWithNoExtension(String inFilePath) {
127 int pos;
128 String tempString;
129
130 tempString = getFileName(inFilePath);
131 pos = tempString.lastIndexOf('.');
132 if (pos >= 0)
133 return tempString.substring(0, pos);
134 else
135 return tempString;
136 }
137
138 /** Deletes all the files contained in directory <CODE>inFile</CODE> using filter
139 * <CODE>filter</CODE>.
140 * @param inFile The directory in which files will be deleted.
141 * @param filter The filter to use.
142 */
143 public static void deleteFileGroup(File inFile, FileFilter filter) {
144 int i, numFiles;
145 File tempFile[];
146
147 tempFile = inFile.listFiles(filter);
148 if (tempFile != null) {
149 numFiles = tempFile.length;
150 for (i=0; i < numFiles; i++)
151 tempFile[i].delete();
152 }
153 }
154 }