KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > nava > informa > utils > FileUtils


1 //
2
// Informa -- RSS Library for Java
3
// Copyright (c) 2002-2003 by Niko Schmuck
4
//
5
// Niko Schmuck
6
// http://sourceforge.net/projects/informa
7
// mailto:niko_schmuck@users.sourceforge.net
8
//
9
// This library is free software.
10
//
11
// You may redistribute it and/or modify it under the terms of the GNU
12
// Lesser General Public License as published by the Free Software Foundation.
13
//
14
// Version 2.1 of the license should be included with this distribution in
15
// the file LICENSE. If the license is not included with this distribution,
16
// you may find a copy at the FSF web site at 'www.gnu.org' or 'www.fsf.org',
17
// or you may write to the Free Software Foundation, 675 Mass Ave, Cambridge,
18
// MA 02139 USA.
19
//
20
// This library is distributed in the hope that it will be useful,
21
// but WITHOUT ANY WARRANTY; without even the implied waranty of
22
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23
// Lesser General Public License for more details.
24
//
25

26
27 // $Id: FileUtils.java,v 1.4 2003/07/26 10:04:38 niko_schmuck Exp $
28

29 package de.nava.informa.utils;
30
31 import java.io.BufferedReader JavaDoc;
32 import java.io.File JavaDoc;
33 import java.io.FileInputStream JavaDoc;
34 import java.io.FileOutputStream JavaDoc;
35 import java.io.FileReader JavaDoc;
36 import java.io.IOException JavaDoc;
37 import java.io.InputStream JavaDoc;
38 import java.io.OutputStream JavaDoc;
39
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 /**
44  * Utility class providing some convenience methods when handling files.
45  */

46 public final class FileUtils {
47
48   private static Log logger = LogFactory.getLog(FileUtils.class);
49
50   private FileUtils() {
51   }
52
53   public static boolean compare(String JavaDoc nameExpected, String JavaDoc nameActual)
54           throws IOException JavaDoc {
55
56     return compare(new File JavaDoc(nameExpected), new File JavaDoc(nameActual));
57   }
58
59   public static boolean compare(File JavaDoc fileExpected, File JavaDoc fileActual)
60           throws IOException JavaDoc {
61
62     BufferedReader JavaDoc readExpected;
63     try {
64       logger.debug("Comparing golden file " + fileExpected +
65                    " to " + fileActual);
66       readExpected = new BufferedReader JavaDoc(new FileReader JavaDoc(fileExpected));
67     } catch (IOException JavaDoc e) {
68       logger.error("Could not read baseline: " + e);
69       return false;
70     }
71     BufferedReader JavaDoc readActual =
72             new BufferedReader JavaDoc(new FileReader JavaDoc(fileActual));
73     return compare(readExpected, readActual);
74   }
75
76   private static boolean compare(BufferedReader JavaDoc readerExpected,
77                                  BufferedReader JavaDoc readerActual)
78           throws IOException JavaDoc {
79
80     String JavaDoc lineExpected = readerExpected.readLine();
81     String JavaDoc lineActual = readerActual.readLine();
82     while (lineExpected != null && lineActual != null) {
83       if (lineExpected == null || lineActual == null) {
84         return false;
85       }
86       if (!lineExpected.equals(lineActual)) {
87         return false;
88       }
89       lineExpected = readerExpected.readLine();
90       lineActual = readerActual.readLine();
91     }
92     readerExpected.close();
93     readerActual.close();
94     return lineExpected == null && lineActual == null;
95   }
96
97   /**
98    * Copies a file from <code>inFile</copy> to <code>outFile</code>.
99    */

100   public static void copyFile(File JavaDoc inFile, File JavaDoc outFile) {
101     try {
102       logger.debug("Copying file " + inFile + " to " + outFile);
103       InputStream JavaDoc in = new FileInputStream JavaDoc(inFile);
104       OutputStream JavaDoc out = new FileOutputStream JavaDoc(outFile);
105       byte[] buf = new byte[8 * 1024];
106       int n;
107       while ((n = in.read(buf)) >= 0) {
108         out.write(buf, 0, n);
109         out.flush();
110       }
111       in.close();
112       out.close();
113     } catch (Exception JavaDoc e) {
114       logger.warn("Error occurred while copying file " + inFile + " to " + outFile);
115       e.printStackTrace();
116     }
117   }
118
119 }
120
Popular Tags