KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > TextFile


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util;
16
17 import java.io.*;
18 import java.util.*;
19
20 /**
21  * Static functions for reading and writing text files as
22  * a single string, and treating a file as an ArrayList.
23  */

24 public class TextFile extends ArrayList {
25
26     /**
27      * Read file as single string.
28      */

29     public static String JavaDoc read(String JavaDoc fileName) throws IOException {
30         File file = new File(fileName);
31         return read(file);
32     }
33
34     /**
35      * Read file as single string.
36      */

37     public static String JavaDoc read(File fileName) throws IOException {
38         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
39         BufferedReader in = null;
40         try {
41             in = new BufferedReader(new FileReader(fileName));
42             String JavaDoc s;
43             while((s = in.readLine()) != null) {
44                 sb.append(s);
45                 sb.append("\n");
46             }
47         } finally {
48             if(in!=null) in.close();
49         }
50         return sb.toString();
51     }
52
53     /**
54      * Write file from a single string.
55      */

56     public static void write(String JavaDoc fileName, String JavaDoc text) throws IOException {
57         File file = new File(fileName);
58         write(file, text);
59     }
60
61     /**
62      * Write file from a single string.
63      */

64     public static void write(File file, String JavaDoc text) throws IOException {
65         PrintWriter out = null;
66         try {
67             out = new PrintWriter(
68                 new BufferedWriter(new FileWriter(file, false)));
69             out.print(text);
70         } finally {
71             if(out!=null) out.close();
72         }
73     }
74
75     public TextFile(String JavaDoc fileName) throws IOException {
76         super(Arrays.asList(read(fileName).split("\n")));
77     }
78
79     /**
80      * Write file from a single string.
81      */

82     public void write(String JavaDoc fileName) throws IOException {
83         PrintWriter out = null;
84         try {
85             out = new PrintWriter(
86                 new BufferedWriter(new FileWriter(fileName)));
87             for(int i = 0; i < size(); i++)
88                 out.println(get(i));
89         } finally {
90             if(out!=null) out.close();
91         }
92     }
93     
94     /**
95      * Read file as single string.
96      */

97     public static String JavaDoc read(String JavaDoc fileName, Object JavaDoc parent)
98             throws IOException {
99         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
100         InputStream is = null;
101         BufferedReader in = null;
102         try {
103             is = parent.getClass().getResourceAsStream(fileName);
104             in = new BufferedReader(new InputStreamReader(is));
105             String JavaDoc s;
106             while((s = in.readLine()) != null) {
107                 sb.append(s);
108                 sb.append("\n");
109             }
110         } finally {
111             if(is!=null) is.close();
112             if(in!=null) in.close();
113         }
114         return sb.toString();
115     }
116 }
117
Popular Tags