KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jorphan > io > TextFile


1 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/io/TextFile.java,v 1.8 2004/02/21 00:46:20 sebb Exp $
2
/*
3  * Copyright 2001-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jorphan.io;
20
21 import java.io.BufferedReader JavaDoc;
22 import java.io.File JavaDoc;
23 import java.io.FileInputStream JavaDoc;
24 import java.io.FileOutputStream JavaDoc;
25 import java.io.FileReader JavaDoc;
26 import java.io.FileWriter JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.InputStreamReader JavaDoc;
29 import java.io.OutputStreamWriter JavaDoc;
30 import java.io.Reader JavaDoc;
31 import java.io.Writer JavaDoc;
32
33 import org.apache.jorphan.logging.LoggingManager;
34 import org.apache.log.Logger;
35
36 /**
37  * Utility class to handle text files as a single lump of text.
38  * <p>
39  * Note this is just as memory-inefficient as handling a text file can be. Use
40  * with restraint.
41  *
42  * @author Giles Cope (gilescope at users.sourceforge.net)
43  * @author Michael Stover (mstover1 at apache.org)
44  * @author <a HREF="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
45  * @version $Revision: 1.8 $ updated on $Date: 2004/02/21 00:46:20 $
46  */

47 public class TextFile extends File JavaDoc
48 {
49     transient private static Logger log = LoggingManager.getLoggerForClass();
50
51     /**
52      * File encoding. null means use the platform's default.
53      */

54     private String JavaDoc encoding= null;
55     
56     /**
57      * Create a TextFile object to handle the named file with the given encoding.
58      *
59      * @param filename File to be read & written through this object.
60      * @param encoding Encoding to be used when reading & writing this file.
61      */

62     public TextFile(File JavaDoc filename, String JavaDoc encoding)
63     {
64         super(filename.toString());
65         setEncoding(encoding);
66     }
67     
68     /**
69      * Create a TextFile object to handle the named file with the platform
70      * default encoding.
71      *
72      * @param filename File to be read & written through this object.
73      */

74     public TextFile(File JavaDoc filename)
75     {
76         super(filename.toString());
77     }
78
79     /**
80      * Create a TextFile object to handle the named file with the platform
81      * default encoding.
82      *
83      * @param filename Name of the file to be read & written through this object.
84      */

85     public TextFile(String JavaDoc filename)
86     {
87         super(filename);
88     }
89
90     /**
91      * Create a TextFile object to handle the named file with the given
92      * encoding.
93      *
94      * @param filename Name of the file to be read & written through this object.
95      * @param encoding Encoding to be used when reading & writing this file.
96      */

97     public TextFile(String JavaDoc filename, String JavaDoc encoding)
98     {
99         super(filename);
100     }
101
102     /**
103      * Create the file with the given string as content -- or replace it's
104      * content with the given string if the file already existed.
105      *
106      * @param body New content for the file.
107      */

108     public void setText(String JavaDoc body)
109     {
110         Writer JavaDoc writer = null;
111         try
112         {
113             if (encoding == null)
114             {
115                 writer = new FileWriter JavaDoc(this);
116             }
117             else
118             {
119                 writer = new OutputStreamWriter JavaDoc(
120                     new FileOutputStream JavaDoc(this),
121                     encoding);
122             }
123             writer.write(body);
124             writer.flush();
125             writer.close();
126         }
127         catch (IOException JavaDoc ioe)
128         {
129             try {
130                 if (writer != null) {
131                     writer.close();
132                 }
133                 } catch (IOException JavaDoc e) {}
134             log.error("", ioe);
135         }
136     }
137
138     /**
139      * Read the whole file content and return it as a string.
140      *
141      * @return the content of the file
142      */

143     public String JavaDoc getText()
144     {
145         String JavaDoc lineEnd = System.getProperty("line.separator");
146         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
147         Reader JavaDoc reader = null;
148         try
149         {
150             if (encoding == null)
151             {
152                 reader= new FileReader JavaDoc(this);
153             }
154             else
155             {
156                 reader= new InputStreamReader JavaDoc(
157                     new FileInputStream JavaDoc(this),
158                     encoding);
159             }
160             BufferedReader JavaDoc br = new BufferedReader JavaDoc(reader);
161             String JavaDoc line = "NOTNULL";
162             while (line != null)
163             {
164                 line = br.readLine();
165                 if (line != null)
166                 {
167                     sb.append(line + lineEnd);
168                 }
169             }
170         }
171         catch (IOException JavaDoc ioe)
172         {
173             log.error("", ioe);
174         }
175         if (reader != null)
176             try {reader.close();} catch (IOException JavaDoc e) {}
177         return sb.toString();
178     }
179
180     /**
181      * @return Encoding being used to read & write this file.
182      */

183     public String JavaDoc getEncoding()
184     {
185         return encoding;
186     }
187
188     /**
189      * @param string Encoding to be used to read & write this file.
190      */

191     public void setEncoding(String JavaDoc string)
192     {
193         encoding= string;
194     }
195 }
196
Popular Tags