01: /*
02: * CoadunationLib: The coaduntion implementation library.
03: * Copyright (C) 2006 Rift IT Contracting
04: *
05: * This library is free software; you can redistribute it and/or
06: * modify it under the terms of the GNU Lesser General Public
07: * License as published by the Free Software Foundation; either
08: * version 2.1 of the License, or (at your option) any later version.
09: *
10: * This library is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13: * Lesser General Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser General Public
16: * License along with this library; if not, write to the Free Software
17: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18: *
19: * TextFile.java
20: *
21: * This class is responsible for reading in the text file it is pointed at.
22: */
23:
24: // the package path
25: package com.rift.coad.lib.common;
26:
27: // java imports
28: import com.rift.coad.lib.common.*;
29: import java.io.File;
30: import java.io.FileReader;
31:
32: /**
33: * This class is responsible for reading in the text file it is pointed at.
34: *
35: * @author Brett Chaldecott
36: */
37: public class TextFile {
38:
39: // private member variables
40: private String textDocument = null;
41:
42: /**
43: * Creates a new instance of TextFile
44: *
45: * @param targetFile The path to the target file.
46: */
47: public TextFile(File targetFile) throws CommonException {
48: try {
49: FileReader fileReader = new FileReader(targetFile);
50: char[] buffer = new char[1024];
51: StringBuffer stringBuffer = new StringBuffer();
52: int length = 0;
53: while ((length = fileReader.read(buffer)) != -1) {
54: stringBuffer.append(buffer, 0, length);
55: }
56: textDocument = stringBuffer.toString();
57: fileReader.close();
58: } catch (Exception ex) {
59: throw new CommonException(
60: "Failed to read in the text document ["
61: + targetFile.getPath() + "] because : "
62: + ex.getMessage(), ex);
63: }
64: }
65:
66: /**
67: * This method returns the text document that has been read in.
68: *
69: * @return The text document that has been read in.
70: */
71: public String getTextDocument() {
72: return textDocument;
73: }
74: }
|