/src/mpv5/utils/files/FileReaderWriter.java
Java | 482 lines | 321 code | 43 blank | 118 comment | 30 complexity | cdc34608ec8d1a07fb68b947044eee6f MD5 | raw file
1/* 2 * This file is part of YaBS. 3 * 4 * YaBS is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * YaBS is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with YaBS. If not, see <http://www.gnu.org/licenses/>. 16 */ 17package mpv5.utils.files; 18 19/** 20 * 21 * 22 */ 23import java.io.File; 24import java.io.BufferedReader; 25import java.io.BufferedWriter; 26import java.io.FileInputStream; 27import java.io.FileOutputStream; 28import java.io.FileReader; 29import java.io.FileWriter; 30import java.io.IOException; 31import java.io.InputStreamReader; 32import java.io.OutputStreamWriter; 33import java.io.Reader; 34import java.nio.charset.Charset; 35import java.util.ArrayList; 36import java.util.logging.Level; 37import java.util.logging.Logger; 38import mpv5.logging.Log; 39import mpv5.utils.arrays.ArrayUtilities; 40 41public class FileReaderWriter { 42 43 private BufferedReader datenstrom; 44 private char[] charArray; 45 private File filer; 46 private FileWriter fw; 47 private BufferedWriter bw; 48 private String zeilenumbruch = "\r\n"; 49 private Charset charset = Charset.forName("UTF-8"); 50 51 /** 52 * A reader/writer helper class for textfiles 53 * @param file 54 */ 55 public FileReaderWriter(File file) { 56 this.filer = file; 57 } 58 59 /** 60 * A reader/writer helper class for textfiles 61 * @param file 62 */ 63 public FileReaderWriter(String file) { 64 filer = new File(file); 65 } 66 67 public FileReaderWriter(File file, String charset) { 68 filer = file; 69 this.charset = Charset.forName(charset); 70 } 71 72 /** 73 * Reads a specific line number 74 * @param skipsign If a line starts with this, skip it. 75 * Skip none if null. 76 * @return The specified line 77 */ 78 public String read1Line(String skipsign) { 79 80 BufferedReader in = null; 81 String s = null; 82 boolean reading = true; 83 84 try { 85 in = new BufferedReader(new InputStreamReader(new FileInputStream(filer))); 86 if (skipsign != null) { 87 s = skipsign; 88 while (reading) { 89 if (s.startsWith(skipsign)) { 90 s = in.readLine(); 91 } else { 92 reading = false; 93 } 94 } 95 } else { 96 s = in.readLine(); 97 } 98 } catch (IOException ex) { 99 mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex); 100 } finally { 101 try { 102 in.close(); 103 } catch (IOException ex) { 104 mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex); 105 } 106 } 107 108 return s; 109 } 110 111 /** 112 * Read a file into an array (line by line) 113 * @return The array representing the file 114 */ 115 public String[] readLines() { 116 BufferedReader in = null; 117 @SuppressWarnings("unchecked") 118 ArrayList<String> arr = new ArrayList(); 119 try { 120 in = new BufferedReader(new InputStreamReader(new FileInputStream(filer))); 121 String s = in.readLine(); 122 123 while (s != null) { 124 arr.add(s); 125 s = in.readLine(); 126 } 127 } catch (IOException ex) { 128 Log.Debug(this, ex.getMessage()); 129 return null; 130 } finally { 131 try { 132 in.close(); 133 } catch (IOException ex) { 134 mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex); 135 } 136 } 137 138 return ArrayUtilities.listToStringArray(arr); 139 } 140 141 /** 142 * Appends the String Array to a file (new line per array field) or creates it if needed 143 * 144 * @param text 145 * @return true is writing successful 146 */ 147 public boolean write(String[] lines) { 148 149 String string = ""; 150 for (int i = 0; i < lines.length; i++) { 151 string += lines[i] + "\n"; 152 } 153 if (string.length() > 0) { 154 if (!write(string)) { 155 return false; 156 } 157 } 158 159 return true; 160 } 161 162 /** 163 * Appends the String to a file (new line) or creates it if needed 164 * 165 * @param text 166 * @return true is writing successful 167 */ 168 public boolean write(String text) { 169 FileWriter out; 170 try { 171 text = zeilenumbruch + text; 172 out = new FileWriter(filer, true); 173 BufferedWriter writer = new BufferedWriter(out); 174 writer.write(text); 175 writer.close(); 176 } catch (IOException ex) { 177 System.out.println(ex.getMessage()); 178 return false; 179 } 180 return true; 181 } 182 183 /** 184 * Appends the String to a file (new line) or creates it if needed 185 * 186 * @param text 187 * @return true is writing successful 188 */ 189 public boolean writeWCharset(String text) { 190 try { 191 text = zeilenumbruch + text; 192 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filer, true), charset)); 193 writer.write(text); 194 writer.close(); 195 } catch (IOException ex) { 196 System.out.println(ex.getMessage()); 197 return false; 198 } 199 return true; 200 } 201 202 /** 203 * Appends the String Array to a file (new line per array field) or creates it if needed. 204 * <li/>Starts from the beginning of the file. 205 * <li/>Skips empty lines. 206 * @param lines 207 * @return 208 */ 209 public boolean write0(String[] lines) { 210 String string = ""; 211 for (int i = 0; i < lines.length; i++) { 212 string += lines[i] + "\n"; 213 } 214 if (string.length() > 0) { 215 if (!write0(string)) { 216 return false; 217 } 218 } 219 220 return true; 221 } 222 223 /** 224 * Appends the String Array to a file (new line per array field) or creates it if needed. 225 * <li/>Starts from the beginning of the file. 226 * <li/>Skips empty lines. 227 * @param lines 228 * @return 229 */ 230 public boolean write0WCharset(String[] lines) { 231 String string = ""; 232 for (int i = 0; i < lines.length; i++) { 233 string += lines[i] + "\n"; 234 } 235 if (string.length() > 0) { 236 if (!write0WCharset(string)) { 237 return false; 238 } 239 } 240 241 return true; 242 } 243 244 /** 245 * Appends the String to a file or creates it if needed. Writes from the beginning of the file. 246 * @param text 247 * @return 248 */ 249 public boolean write0WCharset(String text) { 250 FileWriter out; 251 try { 252 String[] txt = this.readLines(); 253 } catch (Exception e) { 254 try { 255 filer.createNewFile(); 256 } catch (IOException ex) { 257 Log.Debug(e); 258 } 259 } 260 try { 261 out = new FileWriter(filer, true); 262 BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filer, true), charset)); 263 writer.write(text); 264 writer.newLine(); 265 writer.close(); 266 } catch (IOException ex) { 267 System.out.println(ex.getMessage()); 268 return false; 269 } 270 return true; 271 } 272 273 /** 274 * Appends the String to a file or creates it if needed. Writes from the beginning of the file. 275 * @param text 276 * @return 277 */ 278 public boolean write0(String text) { 279 FileWriter out; 280 try { 281 String[] txt = this.readLines(); 282 } catch (Exception e) { 283 try { 284 filer.createNewFile(); 285 } catch (IOException ex) { 286 Log.Debug(e); 287 } 288 } 289 try { 290 out = new FileWriter(filer, true); 291 BufferedWriter writer = new BufferedWriter(out); 292 writer.write(text); 293 writer.newLine(); 294 writer.close(); 295 } catch (IOException ex) { 296 System.out.println(ex.getMessage()); 297 return false; 298 } 299 return true; 300 } 301 302 /** 303 * Creates a file and writes the String into it - \ 304 * deletes existing files! 305 * 306 * @param text 307 * @return true is writing successful 308 */ 309 public boolean writeOnce(String text) { 310 try { 311 312 if (filer.exists()) { 313 filer.delete(); 314 } 315 316 fw = new FileWriter(filer); 317 bw = new BufferedWriter(fw); 318 bw.write(text); 319 bw.close(); 320 } catch (IOException ex) { 321 System.out.println(ex.getMessage()); 322 return false; 323 } 324 325 return true; 326 } 327 328 /** 329 * Reads a whole text file into a String 330 * @return a string with text of file 331 */ 332 public String read() { 333 try { 334 int charArrayLaenge; 335 //Wie heist die Datei? 336 File dateiHandle = filer; 337 //Wie lange ist diese Datei? 338 long datenStromLaenge = dateiHandle.length() + 1; 339 charArrayLaenge = (int) datenStromLaenge - 1; 340 charArray = new char[charArrayLaenge]; 341 try { 342 FileReader dateiInhalt = new FileReader(dateiHandle); 343 datenstrom = new BufferedReader(dateiInhalt); 344 datenstrom.read(charArray, 0, charArrayLaenge); 345 } catch (IOException e) { 346 Log.Debug(e); 347 } 348 datenstrom.close(); 349 } catch (Exception ex) { 350 Log.Debug(ex); 351 return "\n" + filer.getPath() + " not found :-( \n"; 352 353 } 354 return new String(charArray); 355 } 356 357 /** 358 * Reads a whole text file into a String 359 * @return a string with text of file 360 */ 361 public String readWCharset() { 362 try { 363 int charArrayLaenge; 364 //Wie heist die Datei? 365 File dateiHandle = filer; 366 //Wie lange ist diese Datei? 367 long datenStromLaenge = dateiHandle.length() + 1; 368 charArrayLaenge = (int) datenStromLaenge - 1; 369 charArray = new char[charArrayLaenge]; 370 try { 371 Reader dateiInhalt = new InputStreamReader(new FileInputStream(dateiHandle), charset); 372 datenstrom = new BufferedReader(dateiInhalt); 373 datenstrom.read(charArray, 0, charArrayLaenge); 374 } catch (IOException e) { 375 Log.Debug(e); 376 } 377 datenstrom.close(); 378 } catch (Exception ex) { 379 Log.Debug(ex); 380 return "\n" + filer.getPath() + " not found :-( \n"; 381 382 } 383 return new String(charArray); 384 } 385 386 /** 387 * Try to delete the file (may fail, though) 388 */ 389 public void deleteFile() { 390 filer.delete(); 391 } 392 393 /** 394 * Write one line representing the given array 395 * @param text 396 * @param sep The separator between the array items 397 * @return 398 */ 399 public boolean writeLine(String[] text, String sep) { 400 String string = ""; 401 for (int i = 0; i < text.length; i++) { 402 string += text[i] + sep; 403 } 404 if (!writeOnce(string.substring(0, string.length() - sep.length()))) { 405 return false; 406 } 407 return true; 408 } 409 410 /** 411 * Write a file representing the given array 412 * @param text 413 * @param sep The separator between the array columns 414 * @param quoted "? 415 * @return 416 */ 417 public boolean write(Object[][] text, String sep, boolean quoted) { 418 419 boolean result = true; 420 for (int i = 0; i < text.length; i++) { 421 String string = ""; 422 for (int j = 0; j < text[i].length; j++) { 423 Object[] t = text[i]; 424 if (quoted) { 425 string += "\"" + String.valueOf(t[j]).replaceAll("\\<.*?\\>", "") + "\"" + sep; 426 } else { 427 string += String.valueOf(t[j]).replaceAll("\\<.*?\\>", "") + sep; 428 } 429 } 430 result = write(string); 431 string = null; 432 } 433 return result; 434 } 435 436 /** 437 * Flush the file (make it an empty file) 438 */ 439 public void flush() { 440 writeOnce(""); 441 } 442 443 public boolean writeWCharset(String[] text) { 444 for (int i = 0; i < text.length; i++) { 445 String string = text[i]; 446 if (string != null) { 447 if (!writeWCharset(string)) { 448 return false; 449 } 450 } 451 } 452 453 return true; 454 } 455 456 public String[] readLinesWCharset() { 457 458 BufferedReader in = null; 459 @SuppressWarnings("unchecked") 460 ArrayList<String> arr = new ArrayList(); 461 try { 462 in = new BufferedReader(new InputStreamReader(new FileInputStream(filer), charset)); 463 String s = in.readLine(); 464 465 while (s != null) { 466 arr.add(s); 467 s = in.readLine(); 468 } 469 } catch (IOException ex) { 470 Log.Debug(this, ex.getMessage()); 471 return null; 472 } finally { 473 try { 474 in.close(); 475 } catch (IOException ex) { 476 mpv5.logging.Log.Debug(ex);//Logger.getLogger(FileReaderWriter.class.getName()).log(Level.SEVERE, null, ex); 477 } 478 } 479 480 return ArrayUtilities.listToStringArray(arr); 481 } 482}