001: package com.jat.util.file;
002:
003: import java.io.BufferedReader;
004: import java.io.BufferedWriter;
005: import java.io.File;
006: import java.io.FileNotFoundException;
007: import java.io.FileReader;
008: import java.io.FileWriter;
009: import java.io.IOException;
010: import java.io.StringWriter;
011: import java.util.Vector;
012:
013: /**
014: * <p>Title: JAT</p>
015: * <p>Description: </p>
016: * <p>Copyright: Copyright (c) 2004 -2005 Stefano Fratini (stefano.fratini@gmail.com)</p>
017: * <p>Distributed under the terms of the GNU Lesser General Public License, v2.1 or later</p>
018: * @author stf
019: * @version 1.1
020: */
021:
022: public class FileUtil {
023:
024: /**
025: * Read file filename and return the content as String
026: * @param file the file to read
027: * @return the content of the file
028: * @throws java.io.FileNotFoundException if the file does not exist or is a directory
029: * @throws java.io.IOException if any I/O error occours
030: */
031: public static String readFile(String file)
032: throws java.io.FileNotFoundException, java.io.IOException {
033: File f = new File(file);
034: return readFile(f);
035: }
036:
037: /**
038: * Read file filename and return the content as String
039: * @param file the file to read
040: * @return the content of the file
041: * @throws java.io.FileNotFoundException if the file does not exist or is a directory
042: * @throws java.io.IOException if any I/O error occours
043: */
044: public static String readFile(File file) throws java.io.IOException {
045: if (!file.exists())
046: throw new FileNotFoundException("File "
047: + file.getAbsolutePath() + " does not exist.");
048: if (file.isDirectory())
049: throw new FileNotFoundException("File "
050: + file.getAbsolutePath() + " is a directory.");
051: BufferedReader br = null;
052: try {
053: br = new java.io.BufferedReader(
054: new java.io.FileReader(file));
055: return read(br);
056: } finally {
057: try {
058: br.close();
059: } catch (Exception ignored) {
060: }
061: }
062: }
063:
064: /**
065: * Read from the reader and return the content as String
066: * @param reader the reader
067: * @return the content of the reader as String
068: * @throws java.io.IOException if any I/O error occours
069: */
070: public static String read(BufferedReader reader)
071: throws java.io.IOException {
072: String line = "";
073: String ret = "";
074: while (line != null) {
075: line = reader.readLine();
076: if (line != null) {
077: ret += line + "\n";
078: }
079: }
080: return ret;
081: }
082:
083: /**
084: * Return the directories names list of the directory dir
085: * @param dir the directory name
086: * @return a Vector of directory names (as String) without path
087: * @throws IOException if any I/O exception eccours
088: */
089: public static Vector getDirectoryNames(String dir)
090: throws IOException {
091: return getFiles(dir, false, true, true);
092: }
093:
094: /**
095: * Return the directories list of the directory dir
096: * @param dir the directory file
097: * @return a Vector of directory (as File)
098: * @throws IOException if any I/O exception eccours
099: */
100: public static Vector getDirectories(String dir) throws IOException {
101: return getFiles(dir, false, true, false);
102: }
103:
104: /**
105: * Return the filenames list of the directory dir
106: * @param dir the directory name
107: * @return a Vector of file names (as String) without path
108: * @throws IOException if any I/O exception eccours
109: */
110: public static Vector getFileNames(String dir) throws IOException {
111: return getFiles(dir, true, false, true);
112: }
113:
114: /**
115: * Return the filename list of the directory dir
116: * @param dir the directory name
117: * @param includeDirectory if true include subdirectories names un the return list
118: * @return a Vector of file names and directories (as String) without path
119: * @throws IOException if any I/O exception eccours
120: */
121: public static Vector getFileNames(String dir,
122: boolean includeDirectory) throws IOException {
123: return getFiles(dir, true, includeDirectory, true);
124: }
125:
126: /**
127: * Return the files list of the directory dir
128: * @param dir the directory name
129: * @return a Vector of files (as File) excluded subdirectories
130: * @throws IOException if any I/O exception eccours
131: */
132: public static Vector getFiles(String dir) throws IOException {
133: return getFiles(dir, true, false, false);
134: }
135:
136: /**
137: * Return the file list of the directory dir
138: * @param dir the directory name
139: * @param includeDirectory if true include subdirectories names un the return list
140: * @return a Vector of files (as File)
141: * @throws IOException if any I/O exception eccours
142: */
143: public static Vector getFiles(String dir, boolean includeDirectory)
144: throws IOException {
145: return getFiles(dir, true, includeDirectory, false);
146: }
147:
148: /**
149: * Return the list of files and/or subdirectories of directory dir.
150: * It can return a Vector of names (java.lang.String) or a Vector of files (java.io.File) depending of names parameter;
151: * it can include in the returned Vector:
152: * <ul>
153: * <li>files of directory dir (if files parameter is true)</li>
154: * <li>subdirectories of directory dir (if includeDir parameter is true)</li>
155: * <li>both files and subdirectories of directory dir (if files and includeDir parameters are both true)</li>
156: * <li>nothing (if files and includeDir parameters are both false)</li>
157: * </ul>
158: * @param dir the directory name
159: * @param files if true the returned vector will contains also files directory dir
160: * @param includeDir if true the returned vector will contains also sub directories of directory dir
161: * @param name if true the returned vector will contains instances of java.io.File class,
162: * otherwise it will contains instances of filname without path (as java.lang.String)
163: * @return a Vector of java.io.File or java.lang.String depending of name parameter
164: * @throws IOException
165: */
166: public static Vector getFiles(String dir, boolean files,
167: boolean includeDir, boolean name) throws IOException {
168: Vector ret = new Vector();
169: File directory = new File(dir);
170: if (directory.exists() && directory.isDirectory()) {
171: File[] fList = directory.listFiles();
172: for (int a = 0; a < fList.length; a++) {
173: File file = fList[a];
174: if ((!file.isDirectory() && files)
175: || (file.isDirectory() && includeDir)) {
176: if (name)
177: ret.addElement(file.getName());
178: else
179: ret.addElement(file);
180: }
181: }
182: } else
183: throw new java.io.IOException("Directory " + dir
184: + "is not a valid directory");
185: return ret;
186: }
187:
188: /**
189: * Create a new file file from the stream sw
190: * @param file the file to save
191: * @param sw the strem
192: * @throws java.io.IOException if any Exception occurs
193: */
194: public static void saveFile(String file, StringWriter sw)
195: throws java.io.IOException {
196: BufferedWriter writer = null;
197: try {
198: writer = new BufferedWriter(new java.io.FileWriter(file));
199: writer.write(sw.toString());
200: writer.flush();
201: } finally {
202: try {
203: writer.close();
204: } catch (Exception ignored) {
205: }
206: }
207: }
208:
209: /**
210: * Create the file file containg string
211: * @param file the file to save
212: * @param string the content of the file
213: * @throws java.io.IOException if any Exception occurs
214: */
215: public static void saveFile(String file, String string)
216: throws java.io.IOException {
217: BufferedWriter writer = null;
218: try {
219: writer = new BufferedWriter(new java.io.FileWriter(file));
220: writer.write(string);
221: writer.flush();
222: } finally {
223: try {
224: writer.close();
225: } catch (Exception ignored) {
226: }
227: }
228: }
229:
230: /**
231: * Create the file file containg bytes
232: * @param file the file to save
233: * @param bytes the content of the file
234: * @throws java.io.IOException if any Exception occurs
235: */
236: public static void saveFile(String file, byte[] bytes)
237: throws java.io.IOException {
238: saveFile(new File(file), bytes);
239: }
240:
241: /**
242: * Create the file file containg bytes
243: * @param file the file to save
244: * @param bytes the content of the file
245: * @throws java.io.IOException if any Exception occurs
246: */
247: public static void saveFile(File file, byte[] bytes)
248: throws java.io.IOException {
249: BufferedWriter writer = null;
250: try {
251: writer = new BufferedWriter(new FileWriter(file));
252: for (int i = 0; i < bytes.length; i++)
253: writer.write(bytes[i]);
254: writer.flush();
255: } finally {
256: try {
257: writer.close();
258: } catch (Exception ignored) {
259: }
260: }
261: }
262:
263: /**
264: * Copy the file sourceFile to the file targetFile.
265: * If sourceFile is a directoty, copy the entire contents of sourceFile into targetFile directory
266: * @param sourceFile the source file or directory
267: * @param targetFile the source file or directory
268: * @param force used to force overwriting of existent file (or files into directory and subdirectories)
269: * @throws FileNotFoundException if sourceFile does not exist
270: * @throws IOException if source file exists and force is false or if any I/O error occours
271: */
272:
273: public static void copy(String sourceFile, String targetFile,
274: boolean force) throws FileNotFoundException, IOException {
275: copy(new File(sourceFile), new File(targetFile), force);
276: }
277:
278: /**
279: * Copy the file sourceFile to the file targetFile.
280: * If sourceFile is a directoty, copy the entire contents of sourceFile into targetFile directory
281: * @param sourceFile the source file or directory
282: * @param targetFile the source file or directory
283: * @param force used to force overwriting of existent file (or files into directory and subdirectories)
284: * @throws FileNotFoundException if sourceFile does not exist
285: * @throws IOException if source file exists and force is false or if any I/O error occours
286: */
287: public static void copy(File sourceFile, File targetFile,
288: boolean force) throws FileNotFoundException, IOException {
289: if (!sourceFile.exists())
290: throw new FileNotFoundException("Source file '"
291: + sourceFile.getAbsolutePath()
292: + "' does not exist.");
293: if (sourceFile.isDirectory())
294: copyDir(sourceFile, targetFile, force);
295: else {
296: if (!force && targetFile.exists())
297: throw new IOException(
298: "Target file '"
299: + targetFile.getAbsolutePath()
300: + "' already exists. Use force option 'true' if you want overwrite it.");
301: BufferedReader reader = null;
302: BufferedWriter writer = null;
303: try {
304: reader = new BufferedReader(new FileReader(sourceFile));
305: writer = new BufferedWriter(new FileWriter(targetFile));
306: int byteRed = reader.read();
307: while (byteRed != -1) {
308: writer.write(byteRed);
309: byteRed = reader.read();
310: }
311: writer.flush();
312: } finally {
313: try {
314: writer.close();
315: } catch (Exception ignored) {
316: }
317: try {
318: reader.close();
319: } catch (Exception ignored) {
320: }
321: }
322: }
323: }
324:
325: /**
326: * Copy the entire directory sourceFile to the directory targetFile.
327: * @param sourceFile the source directory
328: * @param targetFile the source directory
329: * @param force used to force overwriting of existent files of directory and subdirectories
330: * @throws FileNotFoundException if sourceFile directory does not exist
331: * @throws IOException if at least a file of targetFile directory exists and force is false or if any I/O error occours
332: */
333: protected static void copyDir(File sourceFile, File targetFile,
334: boolean force) throws FileNotFoundException, IOException {
335: if (targetFile.exists() && targetFile.isDirectory())
336: new IOException("Target file '"
337: + targetFile.getAbsolutePath()
338: + "' is not a valid directory.");
339: if (!targetFile.exists())
340: targetFile.mkdirs();
341: File[] files = sourceFile.listFiles();
342: for (int i = 0; i < files.length; i++) {
343: File file = files[i];
344: copy(file, new File(targetFile.getAbsolutePath()
345: + File.separator + file.getName()), force);
346: }
347: }
348:
349: /**
350: * Delete the entire directory dir (if exists)
351: * @param dir the directory
352: * @throws IOException if dir is not a directory or if any I/O error occours
353: */
354: public static void deleteDirectory(File dir) throws IOException {
355: if (!dir.exists())
356: return;
357: if (!dir.isDirectory())
358: throw new IOException("File " + dir.getAbsolutePath()
359: + " is not a valid directory.");
360: File[] files = dir.listFiles();
361: for (int i = 0; i < files.length; i++) {
362: File f = files[i];
363: if (f.isDirectory())
364: deleteDirectory(f);
365: else
366: f.delete();
367: }
368: dir.delete();
369: }
370: }
|