KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > util > FileUtil


1 /**
2  * Copyright 2003-2006 the original author or authors.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6
7        http://www.apache.org/licenses/LICENSE-2.0
8
9   * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */

15
16 package com.jdon.util;
17
18 import java.io.*;
19
20
21 public class FileUtil {
22
23   private static String JavaDoc ENCODING = "UTF-8";
24
25   /**
26    * write the content to a file;
27    * @param output
28    * @param content
29    * @throws Exception
30    */

31   public static void createFile(String JavaDoc output, String JavaDoc content) throws Exception JavaDoc {
32     OutputStreamWriter fw = null;
33     PrintWriter out = null;
34     try {
35       if (ENCODING == null)
36           ENCODING = PropsUtil.ENCODING;
37
38       fw = new OutputStreamWriter(new FileOutputStream(
39           output), ENCODING);
40       out = new PrintWriter(fw);
41       out.print(content);
42     } catch (Exception JavaDoc ex) {
43       throw new Exception JavaDoc(ex);
44     }finally{
45       if (out != null)
46           out.close();
47       if (fw != null)
48           fw.close();
49     }
50
51   }
52
53   /**
54    * read the content from a file;
55    * @param output
56    * @param content
57    * @throws Exception
58    */

59   public static String JavaDoc readFile(String JavaDoc input) throws Exception JavaDoc {
60     char[] buffer = new char[4096];
61     int len = 0;
62     StringBuffer JavaDoc content = new StringBuffer JavaDoc(4096);
63
64     if (ENCODING == null)
65           ENCODING = PropsUtil.ENCODING;
66     InputStreamReader fr = null;
67     BufferedReader br = null;
68     try {
69       fr = new InputStreamReader(new FileInputStream(input), ENCODING);
70       br = new BufferedReader(fr);
71       while ( (len = br.read(buffer)) > -1) {
72         content.append(buffer, 0, len);
73       }
74     } catch (Exception JavaDoc e) {
75       throw new Exception JavaDoc(e);
76     }finally{
77       if (br != null)
78          br.close();
79       if (fr != null)
80          fr.close();
81     }
82     return content.toString();
83   }
84
85   /**
86    * This class moves an input file to output file
87    *
88    * @param String input file to move from
89    * @param String output file
90    *
91    */

92   public static void move(String JavaDoc input, String JavaDoc output) throws Exception JavaDoc {
93     File inputFile = new File(input);
94     File outputFile = new File(output);
95     try {
96       inputFile.renameTo(outputFile);
97     } catch (Exception JavaDoc ex) {
98       throw new Exception JavaDoc("Can not mv" + input + " to " + output +
99                           ex.getMessage());
100     }
101   }
102
103   /**
104    * This class copies an input file to output file
105    *
106    * @param String input file to copy from
107    * @param String output file
108    */

109   public static boolean copy(String JavaDoc input, String JavaDoc output) throws Exception JavaDoc {
110     int BUFSIZE = 65536;
111     FileInputStream fis = new FileInputStream(input);
112     FileOutputStream fos = new FileOutputStream(output);
113
114     try {
115       int s;
116       byte[] buf = new byte[BUFSIZE];
117       while ( (s = fis.read(buf)) > -1) {
118         fos.write(buf, 0, s);
119       }
120
121     } catch (Exception JavaDoc ex) {
122       throw new Exception JavaDoc("makehome" + ex.getMessage());
123     } finally {
124       fis.close();
125       fos.close();
126     }
127     return true;
128   }
129
130   /**
131    * create a directory
132    * @param home
133    * @throws Exception
134    */

135   public static void makehome(String JavaDoc home) throws Exception JavaDoc {
136     File homedir = new File(home);
137     if (!homedir.exists()) {
138       try {
139         homedir.mkdirs();
140       } catch (Exception JavaDoc ex) {
141         throw new Exception JavaDoc("Can not mkdir :" + home +
142                             " Maybe include special charactor!");
143       }
144     }
145   }
146
147
148   /**
149    * This class copies an input files of a directory to another directory not include subdir
150    *
151    * @param String sourcedir the directory to copy from such as:/home/bqlr/images
152    * @param String destdir the target directory
153    */

154   public static void CopyDir(String JavaDoc sourcedir, String JavaDoc destdir) throws Exception JavaDoc {
155     File dest = new File(destdir);
156     File source = new File(sourcedir);
157
158     String JavaDoc[] files = source.list();
159     try {
160       makehome(destdir);
161     } catch (Exception JavaDoc ex) {
162       throw new Exception JavaDoc("CopyDir:" + ex.getMessage());
163     }
164
165     for (int i = 0; i < files.length; i++) {
166       String JavaDoc sourcefile = source + File.separator + files[i];
167       String JavaDoc destfile = dest + File.separator + files[i];
168       File temp = new File(sourcefile);
169       if (temp.isFile()) {
170         try {
171           copy(sourcefile, destfile);
172         } catch (Exception JavaDoc ex) {
173           throw new Exception JavaDoc("CopyDir:" + ex.getMessage());
174         }
175       }
176     }
177   }
178
179   /**
180    * This class del a directory recursively,that means delete all files and directorys.
181    *
182    * @param File directory the directory that will be deleted.
183    */

184   public static void recursiveRemoveDir(File directory) throws Exception JavaDoc {
185     if (!directory.exists())
186       throw new IOException(directory.toString() + " do not exist!");
187
188     String JavaDoc[] filelist = directory.list();
189     File tmpFile = null;
190     for (int i = 0; i < filelist.length; i++) {
191       tmpFile = new File(directory.getAbsolutePath(), filelist[i]);
192       if (tmpFile.isDirectory()) {
193         recursiveRemoveDir(tmpFile);
194       } else if (tmpFile.isFile()) {
195         try {
196           tmpFile.delete();
197         } catch (Exception JavaDoc ex) {
198           throw new Exception JavaDoc(tmpFile.toString() + " can not be deleted " +
199                               ex.getMessage());
200         }
201       }
202     }
203     try {
204       directory.delete();
205     } catch (Exception JavaDoc ex) {
206       throw new Exception JavaDoc(directory.toString() + " can not be deleted " +
207                           ex.getMessage());
208     } finally {
209       filelist = null;
210     }
211   }
212
213 }
214
Popular Tags