01: package com.jamonapi.utils;
02:
03: /** Reusable Utilities used for File manipulations such as reading a file as a String. **/
04:
05: import java.io.*;
06:
07: import com.jamonapi.*;
08:
09: public class FileUtils extends java.lang.Object {
10:
11: /**
12:
13: * Read text files contents in as a String.
14:
15: * Sample Call:
16:
17: * String contents=FileUtils.getFileContents("autoexec.bat");
18:
19: **/
20:
21: public static String getFileContents(String fileName)
22: throws FileNotFoundException, IOException {
23:
24: Monitor mon = MonitorFactory
25: .start("com.jamonapi.utils.FileUtils-getFileContents()");
26:
27: final int EOF = -1;
28:
29: StringBuffer fileContents = new StringBuffer();
30:
31: BufferedReader inputStream = null;
32:
33: // Loop through text file storing contents of the file in a string buffer and return the files
34:
35: // contents to the caller.
36:
37: try {
38:
39: inputStream = new BufferedReader(new FileReader(fileName));
40:
41: int inputChar = inputStream.read();
42:
43: while (inputChar != EOF) {
44:
45: fileContents.append((char) inputChar);
46:
47: inputChar = inputStream.read();
48:
49: }
50:
51: }
52:
53: finally {
54:
55: if (inputStream != null) {
56:
57: inputStream.close();
58:
59: }
60:
61: mon.stop();
62:
63: }
64:
65: return fileContents.toString();
66:
67: }
68:
69: }
|