001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/framework/util/FileUtils.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2008 by:
006 EXSE, Department of Geography, University of Bonn
007 http://www.giub.uni-bonn.de/deegree/
008 lat/lon GmbH
009 http://www.lat-lon.de
010
011 This library is free software; you can redistribute it and/or
012 modify it under the terms of the GNU Lesser General Public
013 License as published by the Free Software Foundation; either
014 version 2.1 of the License, or (at your option) any later version.
015
016 This library is distributed in the hope that it will be useful,
017 but WITHOUT ANY WARRANTY; without even the implied warranty of
018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019 Lesser General Public License for more details.
020
021 You should have received a copy of the GNU Lesser General Public
022 License along with this library; if not, write to the Free Software
023 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024
025 Contact:
026
027 Andreas Poth
028 lat/lon GmbH
029 Aennchenstr. 19
030 53115 Bonn
031 Germany
032 E-Mail: poth@lat-lon.de
033
034 Prof. Dr. Klaus Greve
035 Department of Geography
036 University of Bonn
037 Meckenheimer Allee 166
038 53115 Bonn
039 Germany
040 E-Mail: greve@giub.uni-bonn.de
041
042
043 ---------------------------------------------------------------------------*/
044 package org.deegree.framework.util;
045
046 import java.io.File;
047 import java.io.FileOutputStream;
048 import java.io.IOException;
049 import java.io.InputStream;
050 import java.io.InputStreamReader;
051 import java.io.OutputStreamWriter;
052 import java.io.RandomAccessFile;
053 import java.io.Reader;
054 import java.net.URL;
055
056 /**
057 * the class offeres several static methods for handling file access
058 *
059 *
060 * @version $Revision: 9339 $
061 * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
062 * @author last edited by: $Author: apoth $
063 *
064 * @version 1.0. $Revision: 9339 $, $Date: 2007-12-27 13:31:52 +0100 (Do, 27 Dez 2007) $
065 *
066 * @since 2.0
067 */
068 public class FileUtils {
069
070 /**
071 * writes the the passed string to a file created using the
072 * passed file name. For writing to the resource an
073 * <code>OutputStreamReader</code> with encoding read from
074 * <code>CharsetUtils.getSystemCharset()</code>.
075 *
076 * @param fileName
077 * @param data
078 * @throws IOException
079 */
080 public static final void writeToFile(String fileName, String data) throws IOException {
081 writeToFile( fileName, data, CharsetUtils.getSystemCharset() );
082 }
083
084 /**
085 * writes the the passed string to a file created using the
086 * passed file name using the defined character encoding.
087 *
088 * @param fileName
089 * @param data
090 * @param encoding
091 * @throws IOException
092 */
093 public static final void writeToFile(String fileName, String data, String encoding) throws IOException {
094 FileOutputStream fos = new FileOutputStream( fileName );
095 OutputStreamWriter osr = new OutputStreamWriter( fos, encoding );
096 osr.write( data );
097 osr.close();
098 }
099
100 /**
101 * appends the passed string to the file identified by the passed name. If the
102 * file does not exist an exception will be thrown.
103 *
104 * @param fileName
105 * @param data
106 * @throws IOException
107 */
108 public static final void appendsToFile(String fileName, String data) throws IOException {
109 File file = new File( fileName );
110 if ( !file.exists() ) {
111 throw new IOException( "file: " + fileName + " does not exist" );
112 }
113 RandomAccessFile raf = new RandomAccessFile( file, "rw" );
114 raf.seek( raf.length() );
115 raf.writeChars( data );
116 raf.close();
117 }
118
119 /**
120 * reads a Text file from its resource. For accessing the resource an
121 * <code>InputStreamReader</code> with encoding read from
122 * <code>CharsetUtils.getSystemCharset()</code>
123 *
124 * @param file
125 * @return
126 * @throws IOException
127 */
128 public static StringBuffer readTextFile(File file) throws IOException {
129 return readTextFile( file.toURL() );
130 }
131
132 /**
133 * reads a Text file from its resource. For accessing the resource an
134 * <code>InputStreamReader</code> with encoding read from
135 * <code>CharsetUtils.getSystemCharset()</code>
136 *
137 * @param url
138 * @return
139 * @throws IOException
140 */
141 public static StringBuffer readTextFile(URL url) throws IOException {
142 return readTextFile( url.openStream() );
143 }
144
145 /**
146 * reads a Text file from its resource. For accessing the resource an
147 * <code>InputStreamReader</code> with encoding read from
148 * <code>CharsetUtils.getSystemCharset()</code>
149 *
150 * @param is
151 * @return
152 * @throws IOException
153 */
154 public static StringBuffer readTextFile(InputStream is) throws IOException {
155 InputStreamReader isr = new InputStreamReader( is, CharsetUtils.getSystemCharset() );
156 return readTextFile( isr );
157 }
158
159 /**
160 * reads a Text file from its resource.
161 *
162 * @param reader
163 * @return
164 * @throws IOException
165 */
166 public static StringBuffer readTextFile(Reader reader) throws IOException {
167 StringBuffer sb = new StringBuffer( 10000 );
168 int c = 0;
169 while ( ( c = reader.read() ) > -1 ) {
170 sb.append( (char)c );
171 }
172 reader.close();
173
174 return sb;
175 }
176
177 }