KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > Utility


1 // $RCSfile: Utility.java,v $
2
//
3
// Project "Principia"
4
//
5
// Michael Meyling
6
// Stoltenstrasse 38
7
// 22119 Hamburg
8
// Germany
9

10 import java.io.*;
11
12
13 /**
14  * A collection of usefull static methods.
15  * @version $Revision: 1.1 $
16  * @author Michael Meyling
17  */

18 public class Utility {
19
20
21    /**
22     * Constructor, should never be called.
23     */

24    private Utility() {
25        super();
26    }
27
28
29    /**
30     * Reads a file and returns the contents as a <code>String</code>.
31     *
32     * @param filename name of the file (could include path)
33     * @return contents of file
34     * @throws IOException if a file exception occured
35     */

36    public static final String JavaDoc loadFile(final String JavaDoc filename)
37            throws IOException {
38
39        String JavaDoc text = null;
40
41        final File f = new File(filename);
42        final int size = (int) f.length();
43        final FileReader in = new FileReader(filename);
44        final char[] data = new char[size];
45        int charsread = 0;
46        while (charsread < size) {
47            charsread += in.read(data, charsread, size-charsread);
48        }
49        in.close();
50        text = new String JavaDoc(data);
51        return text;
52    }
53
54
55    /**
56     * Reads contents of a file into a string buffer.
57     *
58     * @param filename name of the file (could include path)
59     * @param buffer buffer to fill with file contents
60     * @throws IOException if a file exception occured
61     */

62    public static final void loadFile(final String JavaDoc filename,
63            final StringBuffer JavaDoc buffer)
64            throws IOException {
65
66        final File f = new File(filename);
67        final int size = (int) f.length();
68        buffer.setLength(0);
69        final FileReader in = new FileReader(filename);
70        final char[] data = new char[size];
71        int charsread = 0;
72        while (charsread < size) {
73            charsread += in.read(data, charsread, size-charsread);
74        }
75        in.close();
76        buffer.insert(0, data);
77    }
78
79
80    /**
81     * Reads contents of a file into a string buffer.
82     *
83     * @param file this file will be loaded
84     * @param buffer buffer to fill with file contents
85     * @throws IOException if a file exception occured
86     */

87    public static final void loadFile(final File file,
88            final StringBuffer JavaDoc buffer)
89            throws IOException {
90
91        final int size = (int) file.length();
92        buffer.setLength(0);
93        final FileReader in = new FileReader(file);
94        final char[] data = new char[size];
95        int charsread = 0;
96        while (charsread < size) {
97            charsread += in.read(data, charsread, size-charsread);
98        }
99        in.close();
100        buffer.insert(0, data);
101    }
102
103
104    /**
105     * Saves a <code>String</code> in a file. In case of an IO error
106     * a message is written to <code>System.out</code>.
107     *
108     * @param filename name of the file (could include path)
109     * @param text data to save in the file
110     * @throws IOException if a file exception occured
111     */

112    public static final void saveFile(String JavaDoc filename, String JavaDoc text)
113            throws IOException {
114        BufferedWriter out = new BufferedWriter(
115            new FileWriter(filename));
116        out.write(text);
117        out.close();
118    }
119
120
121    /**
122     * Saves a <code>String</code> in a file. In case of an IO error
123     * a message is written to <code>System.out</code>.
124     *
125     * @param filename name of the file (could include path)
126     * @param text data to save in the file
127     * @throws IOException if a file exception occured
128     */

129    public static final void saveFile(String JavaDoc filename, StringBuffer JavaDoc text)
130            throws IOException {
131        BufferedWriter out = new BufferedWriter(
132            new FileWriter(filename));
133        out.write(text.toString());
134        out.close();
135    }
136
137
138    /**
139     * Quotes a <code>String</code>. If no quotes exist in the
140     * <code>String</code>, a quote character is appended at the
141     * beginning and the end of the <code>String</code>.
142     *
143     * @param unquoted the ungequoted" <code>String</code>
144     * @return quoted <code>String</code>
145     * @throws NullPointerException if <code>unquoted == null</code>
146     */

147    public static final String JavaDoc quote(String JavaDoc unquoted) {
148
149        String JavaDoc result = new String JavaDoc("\"");
150
151        for (int i = 0; i < unquoted.length(); i++) {
152            if (unquoted.charAt(i) == '\"') {
153                result += "\"\"";
154            } else {
155                result += unquoted.charAt(i);
156            }
157        }
158        result += '\"';
159        return result;
160    }
161
162
163
164    /**
165     * Tests if given <code>String</code> begins with a letter and contains
166     * only letters and digits.
167     *
168     * @param text test this
169     * @return is <code>text</code> only made of letters and digits and has
170     * a leading letter?
171     * @throws NullPointerException if <code>text == null</code>
172     */

173    public static final boolean isLetterDigitString(final String JavaDoc text) {
174        if (text.length() <= 0) {
175            return false;
176        }
177        if (!Character.isLetter(text.charAt(0))) {
178            return false;
179        }
180        for (int i = 1; i < text.length(); i++) {
181            if (!Character.isLetterOrDigit(text.charAt(i))) {
182                return false;
183            }
184        }
185        return true;
186    }
187
188
189    /**
190     * Replaces all occurences of <code>search</code> in <code>text</code>
191     * by <code>replace</code> and returns the result.
192     *
193     * @param text text to work on
194     * @param search replace this text by <code>replace</code>
195     * @param replace replacement for <code>search</code>
196     * @return resulting string
197     */

198    public final static String JavaDoc replace(final String JavaDoc text,
199            final String JavaDoc search, final String JavaDoc replace) {
200
201        final StringBuffer JavaDoc result = new StringBuffer JavaDoc();
202        int pos1 = 0;
203        int pos2;
204        final int len = search.length();
205        while (0 <= (pos2 = text.indexOf(search, pos1))) {
206            result.append(text.substring(pos1, pos2));
207            result.append(replace);
208            pos1 = pos2 + len;
209        }
210        if (pos1 < text.length()) {
211            result.append(text.substring(pos1));
212        }
213        return result.toString();
214    }
215
216
217    /**
218     * Waits til a '\n' was read from System.in.
219     */

220    public final static void waitln() {
221        System.out.println("\n..press <return> to continue");
222        try {
223            (new java.io.BufferedReader JavaDoc(new java.io.InputStreamReader JavaDoc(
224                System.in))).readLine();
225        } catch (IOException e) {
226        }
227    }
228
229
230    /**
231     * Get amount of spaces.
232     *
233     * @param length number of spaces
234     * @return String containig exactly <code>number</code> spaces
235     */

236    public final static StringBuffer JavaDoc getSpaces(int length) {
237        final StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(length);
238        for (int i = 0; i < length; i++) {
239            buffer.append(' ');
240        }
241        return buffer;
242    }
243
244
245 }
Popular Tags