KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > FileUtil


1 package com.bull.eclipse.jonas.utils;
2
3 /*
4  * (c) Copyright Bull SA 2003.
5  * All Rights Reserved.
6  */

7  
8 import java.io.BufferedInputStream JavaDoc;
9 import java.io.BufferedOutputStream JavaDoc;
10 import java.io.BufferedReader JavaDoc;
11 import java.io.File JavaDoc;
12 import java.io.FileInputStream JavaDoc;
13 import java.io.FileOutputStream JavaDoc;
14 import java.io.FileWriter JavaDoc;
15 import java.io.IOException JavaDoc;
16 import java.io.InputStreamReader JavaDoc;
17
18 import com.bull.eclipse.jonas.JonasLauncherPlugin;
19
20
21 /**
22  * Utility class for Files operation
23  */

24  
25 public class FileUtil {
26
27     public static String JavaDoc readTextFile(File JavaDoc f) throws IOException JavaDoc {
28         
29         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
30         
31         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(f)));
32         String JavaDoc inputLine;
33         while ((inputLine = in.readLine()) != null) {
34             buf.append(inputLine);
35             buf.append('\n');
36         }
37         
38         in.close();
39         return buf.toString();
40         
41     }
42     
43     public static void toTextFile(File JavaDoc f, String JavaDoc content) throws IOException JavaDoc {
44         FileWriter JavaDoc out = new FileWriter JavaDoc(f);
45         out.write(content);
46         out.close();
47     }
48     
49
50     public static void copy(String JavaDoc inputFilename, String JavaDoc outputFilename) throws IOException JavaDoc {
51         FileUtil.copy(new File JavaDoc(inputFilename), new File JavaDoc(outputFilename));
52     }
53
54     /**
55      * Copie un fichier vers un autre fichier ou un répertoire vers un autre répertoire
56      */

57     public static void copy(File JavaDoc input, File JavaDoc output) throws IOException JavaDoc {
58         if(input.isDirectory() && output.isDirectory()) {
59             FileUtil.copyDir(input, output);
60         } else {
61             FileUtil.copyFile(input, output);
62         }
63     }
64     
65     /**
66      * Copie un fichier vers un autre
67      */

68     public static void copyFile(File JavaDoc inputFile, File JavaDoc outputFile) throws IOException JavaDoc {
69         JonasLauncherPlugin.log(inputFile.toString());
70         JonasLauncherPlugin.log(outputFile.toString());
71         BufferedInputStream JavaDoc fr = new BufferedInputStream JavaDoc(new FileInputStream JavaDoc(inputFile));
72         BufferedOutputStream JavaDoc fw = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(outputFile));
73         byte[] buf = new byte[8192];
74         int n;
75         while((n = fr.read(buf)) >= 0)
76             fw.write(buf,0,n);
77         fr.close();
78         fw.close();
79     }
80
81     /**
82      * Copie un répertoire dans un autre
83      */

84     public static void copyDir(File JavaDoc inputDir, File JavaDoc outputDir) throws IOException JavaDoc {
85         File JavaDoc[] files = inputDir.listFiles();
86         for(int i=0; i<files.length; i++) {
87             File JavaDoc destFile = new File JavaDoc(outputDir.getAbsolutePath() + File.separator + files[i].getName());
88             if(!destFile.exists()) {
89                 if(files[i].isDirectory()) {
90                     destFile.mkdir();
91                 }
92             }
93             FileUtil.copy(files[i], destFile);
94         }
95     }
96     
97
98     /**
99      * return true if the directory contains files with the extension
100      */

101     
102     public static boolean dirContainsFiles(File JavaDoc dir, String JavaDoc extension, boolean recursive) {
103         File JavaDoc[] files = dir.listFiles();
104         for(int i=0; i<files.length; i++) {
105             if(files[i].isFile() && files[i].getName().endsWith(extension))
106                 return true;
107             if(recursive && files[i].isDirectory())
108                 return FileUtil.dirContainsFiles(files[i], extension, recursive);
109         }
110         
111         return false;
112     }
113
114     /**
115      *
116      */

117     public static String JavaDoc readPropertyInXMLFile(File JavaDoc file, String JavaDoc property) throws IOException JavaDoc {
118         String JavaDoc content = FileUtil.readTextFile(file);
119         int startTagIdx = content.indexOf("<" + property + ">");
120         int endTagIdx = content.indexOf("</" + property + ">");
121         if (startTagIdx == -1)
122             throw new IOException JavaDoc("Property " + property + " not found in file " + file);
123             
124         return content.substring(startTagIdx + property.length() + 2, endTagIdx);
125     }
126
127     
128     /**
129      * Recursive delete of a directory.<br>
130      * The directory itself will be deleted
131      */

132     public static void removeDir(File JavaDoc dir) throws IOException JavaDoc {
133         File JavaDoc[] files = dir.listFiles();
134         for (int i = 0; i < files.length; i++) {
135             if(files[i].isDirectory()) {
136                 FileUtil.removeDir(files[i]);
137             } else {
138                 files[i].delete();
139             }
140         }
141         dir.delete();
142     }
143
144 }
145
Popular Tags