KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > base > util > FileUtil


1 /*
2  * $Id: FileUtil.java 7217 2006-04-06 18:55:22Z jaz $
3  *
4  * Copyright (c) 2001-2006 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.base.util;
26
27 import java.io.BufferedWriter JavaDoc;
28 import java.io.File JavaDoc;
29 import java.io.FileWriter JavaDoc;
30 import java.io.IOException JavaDoc;
31 import java.io.Writer JavaDoc;
32 import java.io.FileNotFoundException JavaDoc;
33 import java.io.FileReader JavaDoc;
34 import java.io.BufferedReader JavaDoc;
35
36 /**
37  * File Utilities
38  *
39  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
40  * @version $Rev: 7217 $
41  * @since 3.5
42  */

43 public class FileUtil {
44
45     public static final String JavaDoc module = FileUtil.class.getName();
46
47     public static void writeString(String JavaDoc fileName, String JavaDoc s) throws IOException JavaDoc {
48         writeString(null, fileName, s);
49     }
50
51     public static void writeString(String JavaDoc path, String JavaDoc name, String JavaDoc s) throws IOException JavaDoc {
52         Writer JavaDoc out = getBufferedWriter(path, name);
53
54         try {
55             out.write(s + System.getProperty("line.separator"));
56         } catch (IOException JavaDoc e) {
57             Debug.logError(e, module);
58             throw e;
59         } finally {
60             if (out != null) {
61                 try {
62                     out.close();
63                 } catch (IOException JavaDoc e) {
64                     Debug.logError(e, module);
65                 }
66             }
67         }
68     }
69
70     public static Writer JavaDoc getBufferedWriter(String JavaDoc path, String JavaDoc name) throws IOException JavaDoc {
71         String JavaDoc fileName = getPatchedFileName(path, name);
72         if (UtilValidate.isEmpty(fileName)) {
73             throw new IOException JavaDoc("Cannot obtain buffered writer for an empty filename!");
74         }
75
76         return new BufferedWriter JavaDoc(new FileWriter JavaDoc(fileName));
77     }
78
79     public static String JavaDoc getPatchedFileName(String JavaDoc path, String JavaDoc fileName) throws IOException JavaDoc {
80         // make sure the export directory exists
81
if (UtilValidate.isNotEmpty(path)) {
82             path = path.replaceAll("\\\\", "/");
83             File JavaDoc parentDir = new File JavaDoc(path);
84             if (!parentDir.exists()) {
85                 if (!parentDir.mkdir()) {
86                     throw new IOException JavaDoc("Cannot create directory for path: " + path);
87                 }
88             }
89
90             // build the filename with the path
91
if (!path.endsWith("/")) {
92                 path = path + "/";
93             }
94             if (fileName.startsWith("/")) {
95                 fileName = fileName.substring(1);
96             }
97             fileName = path + fileName;
98         }
99
100         return fileName;
101     }
102
103     public static StringBuffer JavaDoc readTextFile(String JavaDoc fileName, boolean newline) throws FileNotFoundException JavaDoc, IOException JavaDoc {
104         File JavaDoc file = new File JavaDoc(fileName);
105         if (!file.exists()) {
106             throw new FileNotFoundException JavaDoc();
107         }
108
109         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
110         BufferedReader JavaDoc in = null;
111         try {
112             in = new BufferedReader JavaDoc(new FileReader JavaDoc(file));
113
114             String JavaDoc str;
115             while ((str = in.readLine()) != null) {
116                 buf.append(str);
117                 if (newline) {
118                     buf.append(System.getProperty("line.separator"));
119                 }
120             }
121         } catch (IOException JavaDoc e) {
122             Debug.logError(e, module);
123             throw e;
124         } finally {
125             if (in != null) {
126                 try {
127                     in.close();
128                 } catch (IOException JavaDoc e) {
129                     Debug.logError(e, module);
130                 }
131             }
132         }
133         
134         return buf;
135     }
136 }
137
Popular Tags