KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > common > TextFile


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
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 JavaDoc;
30 import java.io.FileReader JavaDoc;
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 JavaDoc 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 JavaDoc targetFile) throws CommonException {
48         try {
49             FileReader JavaDoc fileReader = new FileReader JavaDoc(targetFile);
50             char[] buffer = new char[1024];
51             StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
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 JavaDoc ex) {
59             throw new CommonException("Failed to read in the text document [" +
60                     targetFile.getPath() + "] because : " + ex.getMessage(),
61                     ex);
62         }
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 JavaDoc getTextDocument() {
72         return textDocument;
73     }
74 }
75
Popular Tags