KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > Util > Tools


1 /**
2  * Tools.java is a part of the SOFA project.
3  * This file was created by pepan on 3.5.2003.
4  */

5 package SOFA.Util;
6
7 import java.io.*;
8
9 /**
10  * Contains a few useful commonly used tools.
11  * @author Petr Panuska and Ladislav Sobr
12  */

13 public class Tools {
14   /**
15    * Deletes the given file. In case the file is a directory, it deletes all
16    * sub-directories/files lying under this directory.
17    * @param file the root directory or file which is going to be deleted.
18    */

19   public static void deleteSubTree (File file) {
20     if (file.isDirectory()) {
21       File[] subFiles = file.listFiles();
22       for (int i = 0; i < subFiles.length; i++) {
23         File subFile = subFiles[i];
24         Tools.deleteSubTree(subFile); // recursively delete sub-files first
25
}
26     }
27     file.delete();
28   }
29   
30   /**
31    * If the 'file' is empty directory, it is deleted. If the parent of the 'file'
32    * becomes empty directory, it is also deleted - and continue recursively.
33    * This deletion stops either on non-empty directory or on 'stopDir'
34    */

35   
36   public static void deleteEmptyDirectoryTree(File file, File stopDir)
37   {
38     while (file != null && file.isDirectory() && !file.equals(stopDir) && file.list().length == 0)
39     {
40       file.delete();
41       file = file.getParentFile();
42     }
43   }
44
45   
46   /**
47    * Moves a file. First it tries to rename the file. If it fails, it simply copies
48    * the content of the source file to the destination file and deletes the source file.
49    * It keeps original time of last modification even in case of copying files.
50    * @param srcFile the source file name (what is to be moved).
51    * @param destFile the destination file (where it is to be moved).
52    */

53   public static void moveFile (File srcFile, File destFile) throws IOException
54   {
55     destFile.getParentFile().mkdirs();
56     if (srcFile.renameTo(destFile) == false) { // try use the renaming method first
57
// if failed, copy the file
58
FileInputStream src = new FileInputStream(srcFile);
59       FileOutputStream dest = new FileOutputStream(destFile);
60
61       copyStream(src, dest);
62
63       src.close();
64       dest.close();
65       destFile.setLastModified(srcFile.lastModified()); // keep the original time of creation
66
srcFile.delete();
67     }
68   }
69   
70   /**
71    * The length of a buffer used for reading data from a stream.
72    */

73   private static final int BUFFER_SIZE = 4096;
74
75   /**
76    * The buffer used for reading data from a stream.
77    */

78   private static final byte[] buffer = new byte[BUFFER_SIZE];
79
80   /**
81    * It copies all data from one stream to the second one using a static buffer.
82    * Since the access to this buffer is synchronized, this method can be used
83    * by many threads at one time with no problems.
84    * @param src the input stream the data is read from.
85    * @param dest the output stream the data is write to.
86    * @throws IOException
87    */

88   public static void copyStream (InputStream src, OutputStream dest) throws IOException {
89     int length;
90     for (; ;) {
91       synchronized (buffer) {
92         length = src.read(buffer);
93         if (length > 0)
94           dest.write(buffer, 0, length);
95         else
96           break;
97       }
98     }
99   }
100
101 }
102
Popular Tags