KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > cms > io > FileHelper


1 /* ===============================================================================
2  *
3  * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4  *
5  * ===============================================================================
6  *
7  * Copyright (C)
8  *
9  * This program is free software; you can redistribute it and/or modify it under
10  * the terms of the GNU General Public License version 2, as published by the
11  * Free Software Foundation. See the file LICENSE.html for more information.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19  * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20  *
21  * ===============================================================================
22  */

23  
24 package org.infoglue.cms.io;
25
26 import java.io.BufferedOutputStream JavaDoc;
27 import java.io.BufferedReader JavaDoc;
28 import java.io.BufferedWriter JavaDoc;
29 import java.io.DataOutputStream JavaDoc;
30 import java.io.File JavaDoc;
31 import java.io.FileInputStream JavaDoc;
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.FileWriter JavaDoc;
34 import java.io.InputStream JavaDoc;
35 import java.io.InputStreamReader JavaDoc;
36 import java.io.OutputStreamWriter JavaDoc;
37 import java.io.PrintWriter JavaDoc;
38 import java.io.Writer JavaDoc;
39
40
41 public class FileHelper
42 {
43     
44     /**
45      * Writes the file to the hard disk. If the file doesn't exist a new file is created.
46      * @author Mattias Bogeblad
47      * @param text The text you want to write to the file.
48      * @param file The file to save to
49      * @param is_append Dictates if the text should be appended to the existing file.
50      * If is_append == true; The text will be added to the existing file.
51      * If is_append == false; The text will overwrite the existing contents of the file.
52      *
53      * @exception java.lang.Exception
54      * @since 2002-12-12
55      */

56  
57     public synchronized static void writeToFile(File JavaDoc file, String JavaDoc text, boolean isAppend) throws Exception JavaDoc
58     {
59         PrintWriter JavaDoc pout = new PrintWriter JavaDoc(new FileWriter JavaDoc(file), isAppend);
60         pout.println(text);
61         pout.close();
62     }
63     
64     /**
65      * Writes the file to the hard disk. If the file doesn't exist a new file is created.
66      * @author Mattias Bogeblad
67      * @param text The text you want to write to the file.
68      * @param file The file to save to
69      * @param is_append Dictates if the text should be appended to the existing file.
70      * If is_append == true; The text will be added to the existing file.
71      * If is_append == false; The text will overwrite the existing contents of the file.
72      *
73      * @exception java.lang.Exception
74      * @since 2002-12-12
75      */

76  
77     //TODO - this is not right.
78
public synchronized static void writeUTF8ToFileSpecial(File JavaDoc file, String JavaDoc text, boolean isAppend) throws Exception JavaDoc
79     {
80         /*
81         FileOutputStream fos = new FileOutputStream(file, isAppend);
82         Writer out = new OutputStreamWriter(fos, "UTF-8");
83         out.write(text);
84         out.flush();
85         out.close();
86         */

87         
88         DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(file, isAppend));
89         dos.writeBytes(text);
90         dos.flush();
91         dos.close();
92         
93     }
94     
95     //TODO - this is not right.
96
public synchronized static void writeUTF8(File JavaDoc file, String JavaDoc text, boolean isAppend) throws Exception JavaDoc
97     {
98         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file, isAppend);
99         Writer JavaDoc out = new OutputStreamWriter JavaDoc(fos, "UTF-8");
100         out.write(text);
101         out.flush();
102         out.close();
103     }
104     
105     
106     public synchronized static void writeUTF8ToFile(File JavaDoc file, String JavaDoc text, boolean isAppend) throws Exception JavaDoc
107     {
108         Writer JavaDoc out = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(new FileOutputStream JavaDoc(file), "UTF8"));
109         out.write(text);
110         out.flush();
111         out.close();
112     }
113     
114     /**
115      * Writes the file to the hard disk. If the file doesn't exist a new file is created.
116      * @author Mattias Bogeblad
117      * @param text The text you want to write to the file.
118      * @param file The file to save to
119      * @param is_append Dictates if the text should be appended to the existing file.
120      * If is_append == true; The text will be added to the existing file.
121      * If is_append == false; The text will overwrite the existing contents of the file.
122      *
123      * @exception java.lang.Exception
124      * @since 2002-12-12
125      */

126  
127     public synchronized static String JavaDoc readUTF8FromFile(File JavaDoc file) throws Exception JavaDoc
128     {
129         BufferedReader JavaDoc in = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(new FileInputStream JavaDoc(file), "UTF8"));
130         String JavaDoc str = in.readLine();
131         
132         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
133         
134         int ch;
135         while ((ch = in.read()) > -1) {
136             sb.append((char)ch);
137         }
138         in.close();
139         
140         return sb.toString();
141     }
142     
143     /**
144      * This method reads a file from the disk and converts it to an byte[].
145      * @author Mattias Bogeblad
146      * @param file The file read bytes from
147      *
148      * @exception java.lang.Exception
149      * @since 2002-12-12
150      */

151     
152     public static byte[] getFileBytes(File JavaDoc file) throws Exception JavaDoc
153     {
154         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
155         byte[] fileBytes = new byte[(int)file.length()];
156         fis.read(fileBytes);
157         fis.close();
158  
159         return fileBytes;
160     }
161     
162     
163     /**
164      * This method reads a file from the disk into a string.
165      * @author Mattias Bogeblad
166      * @param file The file reads from
167      *
168      * @exception java.lang.Exception
169      * @since 2002-12-12
170      */

171     
172     public static String JavaDoc getFileAsString(File JavaDoc file) throws Exception JavaDoc
173     {
174         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
175         
176         FileInputStream JavaDoc fis = new FileInputStream JavaDoc(file);
177         int c;
178         while((c = fis.read()) != -1)
179         {
180             sb.append((char)c);
181         }
182         
183         fis.close();
184         
185         return sb.toString();
186     }
187
188
189     /**
190      * This method reads a file from the disk into a string.
191      * @author Mattias Bogeblad
192      * @param file The file reads from
193      *
194      * @exception java.lang.Exception
195      * @since 2002-12-12
196      */

197     
198     public static String JavaDoc getStreamAsString(InputStream JavaDoc inputStream) throws Exception JavaDoc
199     {
200         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
201         
202         if(inputStream != null)
203         {
204             int c;
205             while((c = inputStream.read()) != -1)
206             {
207                 sb.append((char)c);
208             }
209             
210             inputStream.close();
211         }
212                 
213         return sb.toString();
214     }
215
216     
217     /**
218      * This method writes a file with data from a byte[].
219      * @author Mattias Bogeblad
220      * @param file The file to save to
221      *
222      * @exception java.lang.Exception
223      * @since 2002-12-12
224      */

225     
226     public static void writeToFile(File JavaDoc file, byte[] data) throws Exception JavaDoc
227     {
228         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc(file);
229         BufferedOutputStream JavaDoc bos = new BufferedOutputStream JavaDoc(fos);
230         for(int i=0; i < data.length; i++)
231         {
232             bos.write(data[i]);
233         }
234         
235         bos.flush();
236         bos.close();
237         fos.close();
238     }
239 }
Popular Tags