01: package org.dbbrowser.util;
02:
03: import java.io.BufferedReader;
04: import java.io.FileNotFoundException;
05: import java.io.IOException;
06:
07: /**
08: * utility file reader
09: * @author Amangat
10: */
11: public class FileReader {
12: /**
13: * Reads a file and reads the contents as a string
14: * @param filename
15: * @return - a string with the contents of the file or an error message
16: */
17: public static String readFromFile(String filename) {
18: String fileContents = "";
19: StringBuffer buffer = new StringBuffer();
20: try {
21: BufferedReader reader = new BufferedReader(
22: new java.io.FileReader(filename));
23: String line = reader.readLine();
24: while (line != null) {
25: buffer.append(line + "\n");
26: line = reader.readLine();
27: }
28: reader.close();
29: fileContents = buffer.toString();
30: } catch (FileNotFoundException exc) {
31: //Cant do anything - show an error message
32: fileContents = exc.getMessage();
33: } catch (IOException exc) {
34: //Cant do anything - show an error message
35: fileContents = exc.getMessage();
36: }
37: return fileContents;
38: }
39: }
|