1 10 import java.io.*; 11 12 13 18 public class Utility { 19 20 21 24 private Utility() { 25 super(); 26 } 27 28 29 36 public static final String loadFile(final String filename) 37 throws IOException { 38 39 String 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 (data); 51 return text; 52 } 53 54 55 62 public static final void loadFile(final String filename, 63 final StringBuffer 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 87 public static final void loadFile(final File file, 88 final StringBuffer 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 112 public static final void saveFile(String filename, String text) 113 throws IOException { 114 BufferedWriter out = new BufferedWriter( 115 new FileWriter(filename)); 116 out.write(text); 117 out.close(); 118 } 119 120 121 129 public static final void saveFile(String filename, StringBuffer 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 147 public static final String quote(String unquoted) { 148 149 String result = new String ("\""); 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 173 public static final boolean isLetterDigitString(final String 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 198 public final static String replace(final String text, 199 final String search, final String replace) { 200 201 final StringBuffer result = new StringBuffer (); 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 220 public final static void waitln() { 221 System.out.println("\n..press <return> to continue"); 222 try { 223 (new java.io.BufferedReader (new java.io.InputStreamReader ( 224 System.in))).readLine(); 225 } catch (IOException e) { 226 } 227 } 228 229 230 236 public final static StringBuffer getSpaces(int length) { 237 final StringBuffer buffer = new StringBuffer (length); 238 for (int i = 0; i < length; i++) { 239 buffer.append(' '); 240 } 241 return buffer; 242 } 243 244 245 }
| Popular Tags
|