KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > utils > FileUtility


1 /*
2  ************************************************************************************
3  * Copyright (C) 2001-2006 Openbravo S.L.
4  * Licensed under the Apache Software License version 2.0
5  * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6  * Unless required by applicable law or agreed to in writing, software distributed
7  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8  * CONDITIONS OF ANY KIND, either express or implied. See the License for the
9  * specific language governing permissions and limitations under the License.
10  ************************************************************************************
11 */

12 package org.openbravo.utils;
13
14 import java.io.*;
15 import org.apache.log4j.Logger;
16
17 /**
18  * @author Fernando Iriazabal
19  * @version $Revision: 1.1 $
20 */

21 public class FileUtility {
22   private String JavaDoc dir;
23   private String JavaDoc filename;
24   static Logger log4j = Logger.getLogger(FileUtility.class);
25
26   public FileUtility() {
27   }
28
29   public FileUtility(String JavaDoc path, String JavaDoc name) throws IOException {
30     this(path, name, true);
31   }
32
33   public FileUtility(String JavaDoc path, String JavaDoc name, boolean newFile) throws IOException {
34     if (path == null || path.equals("")) throw new IllegalArgumentException JavaDoc("directory cannot be null");
35     if (name == null || name.equals("")) throw new IllegalArgumentException JavaDoc("file name cannot be null");
36
37     File fpath = new File(path);
38     if(!fpath.isDirectory())
39       throw new IllegalArgumentException JavaDoc("Not a directory: " + path);
40     if(!fpath.canWrite())
41       throw new IllegalArgumentException JavaDoc("Not writable: " + path);
42
43     if (newFile) {
44       File file = new File(path, name);
45       if (file.canRead()) throw new IllegalArgumentException JavaDoc("file: " + path + "\\" + name + " allready exists");
46     }
47
48     dir = path;
49     filename = name;
50   }
51
52   public boolean ByteArrayToFile(ByteArrayOutputStream in) throws IOException {
53     if (in==null) return false;
54     File f = new File(dir, filename);
55     FileOutputStream fos = new FileOutputStream(f);
56     in.writeTo(fos);
57     fos.close();
58     return true;
59   }
60
61   public boolean StringToFile(String JavaDoc in) throws IOException {
62     if (in==null) return false;
63     File f = new File(dir, filename);
64     FileWriter fileWriterData = new FileWriter(f);
65     PrintWriter printWriterData = new PrintWriter(fileWriterData);
66     printWriterData.print(in);
67     printWriterData.close();
68     fileWriterData.close();
69     return true;
70   }
71
72   public void dumpFile(OutputStream outputstream) {
73     byte abyte0[] = new byte[4096];
74     try {
75       BufferedInputStream bufferedinputstream = new BufferedInputStream(new FileInputStream(new File(dir, filename)));
76       int i;
77       while((i = bufferedinputstream.read(abyte0, 0, 4096)) != -1)
78         outputstream.write(abyte0, 0, i);
79       bufferedinputstream.close();
80     }
81     catch(Exception JavaDoc exception) { }
82   }
83
84   public boolean deleteFile() throws IOException {
85     File f = new File(dir, filename);
86     return f.delete();
87   }
88
89   public int copy(File source, File destiny, DirFilter dirFilter, boolean discardHidden, boolean overwrite) throws Exception JavaDoc {
90     File[] list;
91     int total = 0;
92     if(dirFilter!=null) list = source.listFiles(dirFilter);
93     else list = source.listFiles();
94     for(int i = 0; i < list.length; i++) {
95       File fileItem = list[i];
96       if (fileItem.isDirectory()) {
97         if (!discardHidden || !fileItem.isHidden()/*!fileItem.getName().startsWith(".")*/) {
98           File faux = new File(destiny, fileItem.getName());
99           faux.mkdir();
100           // if it is a directory then it is recursively listed
101
total += copy(fileItem, faux, dirFilter, discardHidden, overwrite);
102         }
103       } else {
104         File fileAux = new File(destiny, fileItem.getName());
105         if (overwrite || !fileAux.exists() || fileItem.lastModified()>fileAux.lastModified()) {
106           copyFile(fileItem, fileAux);
107           total++;
108         }
109       }
110     }
111     return total;
112   }
113
114   public void copyFile(File in, File out) throws Exception JavaDoc {
115     //FileInputStream fis = new FileInputStream(in);
116
BufferedReader fileBuffer = new BufferedReader(new FileReader(in));
117     FileOutputStream fos = new FileOutputStream(out);
118     OutputStreamWriter printWriterData = new OutputStreamWriter(fos, "UTF-8");
119     String JavaDoc nextLine = fileBuffer.readLine();
120     while (nextLine != null) {
121       printWriterData.write(nextLine);
122       printWriterData.write("\n");
123       nextLine = fileBuffer.readLine();
124     }
125     printWriterData.flush();
126     fos.close();
127     fileBuffer.close();
128     /*while((i=fis.read(buf))!=-1) {
129       fos.write(buf, 0, i);
130     }
131     fis.close();
132     fos.close();*/

133   }
134
135   public void delete(File source) throws Exception JavaDoc {
136     File[] list = source.listFiles();
137     for(int i = 0; i < list.length; i++) {
138       File fileItem = list[i];
139       if (fileItem.isDirectory()) {
140         delete(fileItem);
141       }
142       fileItem.delete();
143     }
144   }
145 }
146
Popular Tags