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