e.g. Calendar Search Help
You must enter a value before pressing Search
jmeter

Class: org.apache.jorphan.io.TextFile   ©

 OK to copy?
001 // $Header: /home/cvs/jakarta-jmeter/src/jorphan/org/apache/jorphan/io/TextFile.java,v 1.8 2004/02/21 00:46:20 sebb Exp $
002 /*
003  * Copyright 2001-2004 The Apache Software Foundation.
004  *
005  * Licensed under the Apache License, Version 2.0 (the "License");
006  * you may not use this file except in compliance with the License.
007  * You may obtain a copy of the License at
008  *
009  *   http://www.apache.org/licenses/LICENSE-2.0
010  *
011  * Unless required by applicable law or agreed to in writing, software
012  * distributed under the License is distributed on an "AS IS" BASIS,
013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  * See the License for the specific language governing permissions and
015  * limitations under the License.
016  * 
017 */
018 
019 package org.apache.jorphan.io;
020 
021 import java.io.BufferedReader;
022 import java.io.File;
023 import java.io.FileInputStream;
024 import java.io.FileOutputStream;
025 import java.io.FileReader;
026 import java.io.FileWriter;
027 import java.io.IOException;
028 import java.io.InputStreamReader;
029 import java.io.OutputStreamWriter;
030 import java.io.Reader;
031 import java.io.Writer;
032 
033 import org.apache.jorphan.logging.LoggingManager;
034 import org.apache.log.Logger;
035 
036 /**
037  * Utility class to handle text files as a single lump of text.
038  * <p>
039  * Note this is just as memory-inefficient as handling a text file can be. Use
040  * with restraint.
041  * 
042  * @author Giles Cope (gilescope at users.sourceforge.net)
043  * @author Michael Stover (mstover1 at apache.org)
044  * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
045  * @version $Revision: 1.8 $ updated on $Date: 2004/02/21 00:46:20 $
046  */
047 public class TextFile extends File
048 {
049     transient private static Logger log = LoggingManager.getLoggerForClass();
050 
051     /**
052      * File encoding. null means use the platform's default.
053      */
Rate054     private String encoding= null;
055     
056     /**
057      * Create a TextFile object to handle the named file with the given encoding.
058      * 
059      * @param filename File to be read & written through this object.
060      * @param encoding Encoding to be used when reading & writing this file.
061      */
062     public TextFile(File filename, String encoding)
063     {
064         super(filename.toString());
065         setEncoding(encoding);
066     }
067     
068     /**
069      * Create a TextFile object to handle the named file with the platform
070      * default encoding.
071      * 
072      * @param filename File to be read & written through this object.
073      */
074     public TextFile(File filename)
075     {
076         super(filename.toString());
077     }
078 
079     /**
080      * Create a TextFile object to handle the named file with the platform
081      * default encoding.
082      * 
083      * @param filename Name of the file to be read & written through this object.
084      */
085     public TextFile(String filename)
086     {
087         super(filename);
088     }
089 
090     /**
091      * Create a TextFile object to handle the named file with the given
092      * encoding.
093      * 
094      * @param filename Name of the file to be read & written through this object.
095      * @param encoding Encoding to be used when reading & writing this file.
096      */
097     public TextFile(String filename, String encoding)
098     {
099         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 body)
109     {
110         Writer writer = null;
111         try
112         {
113             if (encoding == null)
114             {
115                 writer = new FileWriter(this);
116             }
117             else
118             {
119                 writer = new OutputStreamWriter(
120                     new FileOutputStream(this),
121                     encoding);
122             }
123             writer.write(body);
124             writer.flush();
125             writer.close();
126         }
127         catch (IOException ioe)
128         {
129             try {
130                 if (writer != null) {
131                     writer.close();
132                 }
133                 } catch (IOException 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 getText()
144     {
145         String lineEnd = System.getProperty("line.separator");
146         StringBuffer sb = new StringBuffer();
147         Reader reader = null;
148         try
149         {
150             if (encoding == null)
151             {
152                 reader= new FileReader(this);
153             }
154             else
155             {
156                 reader= new InputStreamReader(
157                     new FileInputStream(this),
158                     encoding);
159             }
160             BufferedReader br = new BufferedReader(reader);
161             String 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 ioe)
172         {
173             log.error("", ioe);
174         }
175         if (reader != null)
176             try {reader.close();} catch (IOException e) {}
177         return sb.toString();
178     }
179 
180     /**
181      * @return Encoding being used to read & write this file.
182      */
183     public String 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 string)
192     {
193         encoding= string;
194     }
195 }

            
All Examples in File:
Example
Line
Rating (found
useful by...)
54 0% of 0