001: /*
002: * FileUtils.java
003: *
004: * Copyright (C) 2002, 2003, 2004, 2005, 2006 Takis Diakoumis
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package org.underworldlabs.util;
023:
024: import java.io.BufferedInputStream;
025: import java.io.BufferedOutputStream;
026: import java.io.BufferedReader;
027: import java.io.File;
028: import java.io.FileInputStream;
029: import java.io.FileOutputStream;
030: import java.io.FileReader;
031: import java.io.FileWriter;
032: import java.io.IOException;
033: import java.io.InputStream;
034: import java.io.ObjectInputStream;
035: import java.io.ObjectOutputStream;
036: import java.io.OutputStream;
037: import java.io.PrintWriter;
038: import java.util.Properties;
039:
040: /* ----------------------------------------------------------
041: * CVS NOTE: Changes to the CVS repository prior to the
042: * release of version 3.0.0beta1 has meant a
043: * resetting of CVS revision numbers.
044: * ----------------------------------------------------------
045: */
046:
047: /**
048: * File access utilities.
049: *
050: * @author Takis Diakoumis
051: * @version $Revision: 1.5 $
052: * @date $Date: 2006/09/17 13:10:19 $
053: */
054: public class FileUtils {
055:
056: private FileUtils() {
057: }
058:
059: public static boolean fileExists(String path) {
060: File file = new File(path);
061: return file.exists();
062: }
063:
064: public static void writeFile(String path, String text)
065: throws IOException {
066: writeFile(new File(path), text, false);
067: }
068:
069: public static void writeFile(String path, String text,
070: boolean append) throws IOException {
071: writeFile(new File(path), text, append);
072: }
073:
074: public static void writeFile(File file, String text)
075: throws IOException {
076: writeFile(file, text, false);
077: }
078:
079: public static void writeFile(File file, String text, boolean append)
080: throws IOException {
081: PrintWriter writer = null;
082: try {
083: writer = new PrintWriter(new FileWriter(file, append), true);
084: writer.println(text);
085: } finally {
086: if (writer != null) {
087: writer.close();
088: }
089: }
090: }
091:
092: public static String loadFile(File file) throws IOException {
093: return loadFile(file, true);
094: }
095:
096: public static String loadFile(String path) throws IOException {
097: return loadFile(new File(path), true);
098: }
099:
100: public static String loadFile(String path, boolean escapeLines)
101: throws IOException {
102: return loadFile(new File(path), escapeLines);
103: }
104:
105: public static String loadFile(File file, boolean escapeLines)
106: throws IOException {
107: FileReader fileReader = null;
108: BufferedReader reader = null;
109:
110: try {
111: fileReader = new FileReader(file);
112: reader = new BufferedReader(fileReader);
113:
114: String value = null;
115: StringBuffer sb = new StringBuffer();
116:
117: while ((value = reader.readLine()) != null) {
118: sb.append(value);
119:
120: if (escapeLines) {
121: sb.append('\n');
122: }
123:
124: }
125:
126: return sb.toString();
127: } finally {
128: if (reader != null) {
129: reader.close();
130: }
131: if (fileReader != null) {
132: fileReader.close();
133: }
134: }
135: }
136:
137: public static String loadResource(String path) throws IOException {
138: InputStream input = null;
139:
140: try {
141: ClassLoader cl = FileUtils.class.getClassLoader();
142:
143: if (cl != null) {
144: input = cl.getResourceAsStream(path);
145: } else {
146: input = ClassLoader.getSystemResourceAsStream(path);
147: }
148:
149: int i = 0;
150: StringBuffer buf = new StringBuffer();
151:
152: while ((i = input.read()) != -1) {
153: buf.append((char) i);
154: }
155:
156: return buf.toString();
157: } finally {
158: if (input != null) {
159: input.close();
160: }
161: }
162: }
163:
164: public static Properties loadProperties(String path,
165: Properties defaults) throws IOException {
166: return loadProperties(new File(path), defaults);
167: }
168:
169: public static Properties loadProperties(File file,
170: Properties defaults) throws IOException {
171: InputStream input = null;
172:
173: try {
174: Properties properties = null;
175:
176: if (defaults != null) {
177: properties = new Properties(defaults);
178: } else {
179: properties = new Properties();
180: }
181:
182: input = new FileInputStream(file);
183: properties.load(input);
184: return properties;
185: } finally {
186: if (input != null) {
187: input.close();
188: }
189: }
190:
191: }
192:
193: public static Properties loadProperties(String path)
194: throws IOException {
195: return loadProperties(new File(path), null);
196: }
197:
198: public static Properties loadProperties(File file)
199: throws IOException {
200: return loadProperties(file, null);
201: }
202:
203: public static void storeProperties(String path,
204: Properties properties, String header) throws IOException {
205: storeProperties(new File(path), properties, header);
206: }
207:
208: public static void storeProperties(File file,
209: Properties properties, String header) throws IOException {
210: FileOutputStream output = null;
211: try {
212: output = new FileOutputStream(file);
213: properties.store(output, header);
214: } finally {
215: if (output != null) {
216: output.close();
217: }
218: }
219: }
220:
221: public static Properties loadPropertiesResource(String path)
222: throws IOException {
223: InputStream input = null;
224:
225: try {
226: ClassLoader cl = FileUtils.class.getClassLoader();
227:
228: if (cl != null) {
229: input = cl.getResourceAsStream(path);
230: } else {
231: input = ClassLoader.getSystemResourceAsStream(path);
232: }
233:
234: Properties properties = new Properties();
235: properties.load(input);
236: input.close();
237: return properties;
238: } finally {
239: if (input != null) {
240: input.close();
241: }
242: }
243: }
244:
245: public static Object readObject(String path) throws IOException {
246: return readObject(new File(path));
247: }
248:
249: public static Object readObject(File file) throws IOException {
250: FileInputStream fileIn = null;
251: BufferedInputStream buffIn = null;
252: ObjectInputStream obIn = null;
253:
254: try {
255: fileIn = new FileInputStream(file);
256: buffIn = new BufferedInputStream(fileIn);
257: obIn = new ObjectInputStream(buffIn);
258: return obIn.readObject();
259: } catch (ClassNotFoundException cExc) {
260: cExc.printStackTrace();
261: return null;
262: } finally {
263: try {
264: if (obIn != null) {
265: obIn.close();
266: }
267: if (buffIn != null) {
268: buffIn.close();
269: }
270: if (fileIn != null) {
271: fileIn.close();
272: }
273: } catch (IOException e) {
274: }
275: }
276: }
277:
278: public static void writeObject(Object object, String path)
279: throws IOException {
280: FileOutputStream fileOut = null;
281: BufferedOutputStream bufferedOut = null;
282: ObjectOutputStream obOut = null;
283:
284: try {
285: fileOut = new FileOutputStream(path);
286: bufferedOut = new BufferedOutputStream(fileOut);
287: obOut = new ObjectOutputStream(bufferedOut);
288: obOut.writeObject(object);
289: } finally {
290: try {
291: if (bufferedOut != null) {
292: bufferedOut.close();
293: }
294: if (obOut != null) {
295: obOut.close();
296: }
297: if (fileOut != null) {
298: fileOut.close();
299: }
300: } catch (IOException e) {
301: }
302: }
303:
304: }
305:
306: /** default buffer read size */
307: private static int defaultBufferSize = 32768;
308:
309: /**
310: * Copies the file at the specified from path to the
311: * specified to path.
312: *
313: * @param from - the from path
314: * @param to - the to path
315: * @throws IOException
316: */
317: public static void copyResource(String from, String to)
318: throws IOException {
319: InputStream in = null;
320: OutputStream out = null;
321:
322: try {
323: ClassLoader cl = FileUtils.class.getClassLoader();
324:
325: if (cl != null) {
326: in = cl.getResourceAsStream(from);
327: } else {
328: in = ClassLoader.getSystemResourceAsStream(from);
329: }
330:
331: out = new FileOutputStream(to);
332:
333: byte[] buffer = new byte[defaultBufferSize];
334: while (true) {
335: synchronized (buffer) {
336: int amountRead = in.read(buffer);
337: if (amountRead == -1) {
338: break;
339: }
340: out.write(buffer, 0, amountRead);
341: }
342: }
343:
344: } finally {
345: if (in != null) {
346: in.close();
347: }
348: if (out != null) {
349: out.close();
350: }
351: }
352: }
353:
354: public static void copyFile(String from, String to)
355: throws IOException {
356: InputStream in = null;
357: OutputStream out = null;
358:
359: try {
360: in = new FileInputStream(from);
361: out = new FileOutputStream(to);
362:
363: byte[] buffer = new byte[defaultBufferSize];
364: while (true) {
365: synchronized (buffer) {
366: int amountRead = in.read(buffer);
367: if (amountRead == -1) {
368: break;
369: }
370: out.write(buffer, 0, amountRead);
371: }
372: }
373:
374: } finally {
375: if (in != null) {
376: in.close();
377: }
378: if (out != null) {
379: out.close();
380: }
381: }
382: }
383:
384: }
|