1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.sf.statcvs.util;
24
25 import java.io.BufferedReader;
26 import java.io.File;
27 import java.io.FileNotFoundException;
28 import java.io.FileOutputStream;
29 import java.io.FileReader;
30 import java.io.FileWriter;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.net.MalformedURLException;
35 import java.net.URL;
36
37 /**
38 * Some helpful file functions
39 * TODO: Remove redundancy, write tests
40 * @author Lukasz Pekacki
41 * @version $Id: FileUtils.java,v 1.21 2009/08/19 22:11:15 benoitx Exp $
42 */
43 public class FileUtils {
44 /**
45 * Copies a file to a specified destination
46 * @param inputName File
47 * @param destination Filename
48 * @throws FileNotFoundException if no input file exists
49 * @throws IOException if cannot read or write
50 */
51 public static void copyFile(final String inputName, final String destination) throws FileNotFoundException, IOException {
52 final File input = new File(inputName);
53 final File outputFile = new File(destination);
54 FileReader in = null;
55 FileWriter out = null;
56 try {
57 in = new FileReader(input);
58 out = new FileWriter(outputFile);
59 int c;
60 while ((c = in.read()) != -1) {
61 out.write(c);
62 }
63 } finally {
64 try {
65 if (in != null) {
66 in.close();
67 }
68 } finally {
69 if (out != null) {
70 out.close();
71 }
72 }
73 }
74 }
75
76 /**
77 * Copy a InputStream into a File
78 * @param in source
79 * @param out destination
80 * @throws FileNotFoundException if not found
81 * @throws IOException if read/write error
82 */
83 public static void copyFile(final InputStream in, final File out) throws FileNotFoundException, IOException {
84 final InputStream fis = in;
85 FileOutputStream fos = null;
86 try {
87 fos = new FileOutputStream(out);
88 final byte[] buf = new byte[1024];
89 int i = 0;
90 while ((i = fis.read(buf)) > 0) {
91 fos.write(buf, 0, i);
92 }
93 } finally {
94 try {
95 fis.close();
96 } finally {
97 if (fos != null) {
98 fos.close();
99 }
100 }
101 }
102 }
103
104 /**
105 * Takes a filename with path and returns just the filename.
106 * @param filename a filename with path
107 * @return just the filename part
108 */
109 public static String getFilenameWithoutPath(final String filename) {
110 final File f = new File(filename);
111 return f.getName();
112 }
113
114 /**
115 * Returns the os dependent path separator
116 * @return String os dependent path separator
117 */
118 public static String getDirSeparator() {
119 return System.getProperty("file.separator");
120 }
121
122 /**
123 * Returns the java path separator
124 * @return String java path separator
125 */
126 public static String getDefaultDirSeparator() {
127
128 return java.io.File.separator;
129 }
130
131 /**
132 * Deletes the ending directory separator of a
133 * given <code>path</code> if there is one and returns
134 * the result.
135 * Otherwise the path is unhandled returned.
136 *
137 * <p>The separator is the one used bye the
138 * underlying operating system and it is the one returned
139 * bye the <code>getDirSeparator()</code> method.
140 *
141 * @param path The <code>path</code> to delete the directory
142 * separator from.
143 * @return The <code>path</code> without the ending
144 * directory separator.
145 * @see net.sf.statcvs.util.FileUtils#getDirSeparator
146 */
147 public static String getPathWithoutEndingSlash(final String path) {
148 if (path.endsWith(getDefaultDirSeparator())) {
149 final int pos = path.lastIndexOf(getDefaultDirSeparator());
150 return path.substring(0, pos);
151 }
152 return path;
153 }
154
155 /**
156 * Concatenates <code>path</code> and filename to an
157 * absolute filename by inserting the system file separator.
158 *
159 * @param path The path to use.
160 * @param filename The filename for concatenation.
161 * @return The concatenated absolute filename.
162 */
163 public static String getAbsoluteName(final String path, final String filename) {
164 return path + getDirSeparator() + filename;
165 }
166
167 /**
168 * Returns the last component of a directory path.
169 * @param path a directory, ending in "/", for example "src/net/sf/statcvs/"
170 * @return the last component of the path, for example "statcvs"
171 */
172 public static String getDirectoryName(final String path) {
173 if ("".equals(path)) {
174 throw new IllegalArgumentException("can't get directory name for root");
175 }
176 final String pathWithoutLastSlash = path.substring(0, path.length() - 1);
177 final int lastSlash = pathWithoutLastSlash.lastIndexOf('/');
178 if (lastSlash == -1) {
179 return pathWithoutLastSlash;
180 }
181 return pathWithoutLastSlash.substring(lastSlash + 1);
182 }
183
184 /**
185 * Returns all but the last component of a directory path
186 * @param path a directory, ending in "/", for example "src/net/sf/statcvs/"
187 * @return all but the last component of the path, for example "src/net/sf/"
188 */
189 public static String getParentDirectoryPath(final String path) {
190 if ("".equals(path)) {
191 throw new IllegalArgumentException("can't get directory name for root");
192 }
193 final String pathWithoutLastSlash = path.substring(0, path.length() - 1);
194 final int lastSlash = pathWithoutLastSlash.lastIndexOf('/');
195 if (lastSlash == -1) {
196 return "";
197 }
198 return pathWithoutLastSlash.substring(0, lastSlash + 1);
199 }
200
201 /**
202 * Read a full file into a string.
203 * @param urlTxt URL of the text to get
204 * @return
205 */
206 public static String readTextFromURL(final String urlTxt) {
207 final StringBuffer buf = new StringBuffer();
208 BufferedReader in = null;
209 try {
210
211 final URL url = new URL(urlTxt);
212
213 in = new BufferedReader(new InputStreamReader(url.openStream()));
214 String str;
215 while ((str = in.readLine()) != null) {
216
217 buf.append(str);
218 }
219 } catch (final MalformedURLException e) {
220 throw new IllegalArgumentException("Mal-Formed URL " + urlTxt);
221 } catch (final IOException e) {
222 throw new IllegalArgumentException("can't get file " + urlTxt);
223 } finally {
224 if (in != null) {
225 try {
226 in.close();
227 } catch (final IOException e) {
228 }
229 }
230 }
231 return buf.toString();
232 }
233 }