OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 Eyeo GmbH |
| 4 * |
| 5 * Adblock Plus is free software: you can redistribute it and/or modify |
| 6 * it under the terms of the GNU General Public License version 3 as |
| 7 * published by the Free Software Foundation. |
| 8 * |
| 9 * Adblock Plus 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 Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
| 16 */ |
| 17 |
| 18 package org.adblockplus.libadblockplus; |
| 19 |
| 20 import java.io.File; |
| 21 import java.io.FileNotFoundException; |
| 22 import java.io.FileOutputStream; |
| 23 import java.io.IOException; |
| 24 import java.util.NoSuchElementException; |
| 25 import java.util.Scanner; |
| 26 import java.util.UUID; |
| 27 |
| 28 public class FileSystemHelper |
| 29 { |
| 30 /** |
| 31 * Read all the file data to string |
| 32 * |
| 33 * @param file path to read data |
| 34 * @return file data |
| 35 * @throws java.io.FileNotFoundException |
| 36 */ |
| 37 public String readFile(File file) throws FileNotFoundException |
| 38 { |
| 39 Scanner scanner = null; |
| 40 try |
| 41 { |
| 42 scanner = new Scanner(file); |
| 43 return scanner.useDelimiter("\\A").next(); |
| 44 } |
| 45 catch (NoSuchElementException e) |
| 46 { |
| 47 return ""; |
| 48 } |
| 49 finally |
| 50 { |
| 51 if (scanner != null) |
| 52 scanner.close(); |
| 53 } |
| 54 } |
| 55 |
| 56 /** |
| 57 * Write data to file (with rewriting) |
| 58 * |
| 59 * @param file file |
| 60 * @param data file data |
| 61 */ |
| 62 public void writeFile(File file, String data) throws IOException |
| 63 { |
| 64 FileOutputStream fos = null; |
| 65 try |
| 66 { |
| 67 fos = new FileOutputStream(file); |
| 68 if (data != null) |
| 69 fos.write(data.getBytes()); |
| 70 } |
| 71 finally |
| 72 { |
| 73 if (fos != null) |
| 74 try |
| 75 { |
| 76 fos.close(); |
| 77 } catch (IOException e) |
| 78 { |
| 79 // ignored |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 /** |
| 85 * Generate unique filename |
| 86 * @param prefix prefix |
| 87 * @param suffix suffix |
| 88 * @return generated unique filename |
| 89 */ |
| 90 public String generateUniqueFileName(String prefix, String suffix) |
| 91 { |
| 92 StringBuilder sb = new StringBuilder(); |
| 93 if (prefix != null) |
| 94 sb.append(prefix); |
| 95 |
| 96 sb.append(UUID.randomUUID().toString()); |
| 97 |
| 98 if (suffix != null) |
| 99 sb.append(suffix); |
| 100 |
| 101 return sb.toString(); |
| 102 } |
| 103 } |
OLD | NEW |