001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. 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.jorphan.util.JOrphanUtils;
035: import org.apache.log.Logger;
036:
037: /**
038: * Utility class to handle text files as a single lump of text.
039: * <p>
040: * Note this is just as memory-inefficient as handling a text file can be. Use
041: * with restraint.
042: *
043: * @author Giles Cope (gilescope at users.sourceforge.net)
044: * @author Michael Stover (mstover1 at apache.org)
045: * @author <a href="mailto:jsalvata@apache.org">Jordi Salvat i Alabart</a>
046: * @version $Revision: 533867 $ updated on $Date: 2007-04-30 23:13:40 +0100 (Mon, 30 Apr 2007) $
047: */
048: public class TextFile extends File {
049: private static final Logger log = LoggingManager
050: .getLoggerForClass();
051:
052: /**
053: * File encoding. null means use the platform's default.
054: */
055: private String encoding = null;
056:
057: /**
058: * Create a TextFile object to handle the named file with the given
059: * encoding.
060: *
061: * @param filename
062: * File to be read & written through this object.
063: * @param encoding
064: * Encoding to be used when reading & writing this file.
065: */
066: public TextFile(File filename, String encoding) {
067: super (filename.toString());
068: setEncoding(encoding);
069: }
070:
071: /**
072: * Create a TextFile object to handle the named file with the platform
073: * default encoding.
074: *
075: * @param filename
076: * File to be read & written through this object.
077: */
078: public TextFile(File filename) {
079: super (filename.toString());
080: }
081:
082: /**
083: * Create a TextFile object to handle the named file with the platform
084: * default encoding.
085: *
086: * @param filename
087: * Name of the file to be read & written through this object.
088: */
089: public TextFile(String filename) {
090: super (filename);
091: }
092:
093: /**
094: * Create a TextFile object to handle the named file with the given
095: * encoding.
096: *
097: * @param filename
098: * Name of the file to be read & written through this object.
099: * @param encoding
100: * Encoding to be used when reading & writing this file.
101: */
102: public TextFile(String filename, String encoding) {
103: super (filename);
104: }
105:
106: /**
107: * Create the file with the given string as content -- or replace it's
108: * content with the given string if the file already existed.
109: *
110: * @param body
111: * New content for the file.
112: */
113: public void setText(String body) {
114: Writer writer = null;
115: try {
116: if (encoding == null) {
117: writer = new FileWriter(this );
118: } else {
119: writer = new OutputStreamWriter(new FileOutputStream(
120: this ), encoding);
121: }
122: writer.write(body);
123: writer.flush();
124: } catch (IOException ioe) {
125: log.error("", ioe);
126: } finally {
127: JOrphanUtils.closeQuietly(writer);
128: }
129: }
130:
131: /**
132: * Read the whole file content and return it as a string.
133: *
134: * @return the content of the file
135: */
136: public String getText() {
137: String lineEnd = System.getProperty("line.separator"); //$NON-NLS-1$
138: StringBuffer sb = new StringBuffer();
139: Reader reader = null;
140: BufferedReader br = null;
141: try {
142: if (encoding == null) {
143: reader = new FileReader(this );
144: } else {
145: reader = new InputStreamReader(
146: new FileInputStream(this ), encoding);
147: }
148: br = new BufferedReader(reader);
149: String line = "NOTNULL"; //$NON-NLS-1$
150: while (line != null) {
151: line = br.readLine();
152: if (line != null) {
153: sb.append(line + lineEnd);
154: }
155: }
156: } catch (IOException ioe) {
157: log.error("", ioe); //$NON-NLS-1$
158: } finally {
159: JOrphanUtils.closeQuietly(br); // closes reader as well
160: }
161:
162: return sb.toString();
163: }
164:
165: /**
166: * @return Encoding being used to read & write this file.
167: */
168: public String getEncoding() {
169: return encoding;
170: }
171:
172: /**
173: * @param string
174: * Encoding to be used to read & write this file.
175: */
176: public void setEncoding(String string) {
177: encoding = string;
178: }
179: }
|