|
| 1 | +package com.fazziclay.javaneoutil; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileReader; |
| 5 | +import java.io.FileWriter; |
| 6 | + |
| 7 | +/** |
| 8 | + * For files :). License: MIT |
| 9 | + * @author FazziCLAY ( <a href="https://fazziclay.github.io">https://fazziclay.github.io</a> ) |
| 10 | + * **/ |
| 11 | +public class FileUtil { |
| 12 | + /** |
| 13 | + * Fix Windows-Linux path (replace / & \\ to File.separator) |
| 14 | + **/ |
| 15 | + private static String fixPathSeparator(@NonNull String path) { |
| 16 | + return path |
| 17 | + .replace("/", File.separator) |
| 18 | + .replace("\\", File.separator); |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * Получить файл из пути, путь чинится Windows-Linux |
| 23 | + **/ |
| 24 | + public static File getFile(String path) { |
| 25 | + return new File(fixPathSeparator(path)); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * <h1>Example</h1> |
| 30 | + * <p>Input: "/test/owo/f.json"</p> |
| 31 | + * <p>Output: "/test/owo"</p> |
| 32 | + **/ |
| 33 | + public static String getParentDirOfFile(String path) { |
| 34 | + path = fixPathSeparator(path); |
| 35 | + int lastSep = path.lastIndexOf(File.separator); |
| 36 | + if (lastSep > 0) { |
| 37 | + return path.substring(0, lastSep) + File.separator; |
| 38 | + } |
| 39 | + return File.separator; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @see FileUtil#getParentDirOfFile(String) |
| 44 | + **/ |
| 45 | + public static File getParentDirOfFile(File file) { |
| 46 | + return new File(getParentDirOfFile(fixPathSeparator(file.getAbsolutePath()))); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @see FileUtil#createDirIfNotExists(File) |
| 51 | + **/ |
| 52 | + public static void createDirIfNotExists(String path) { |
| 53 | + createDirIfNotExists(getFile(path)); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Создать дирректорию если она не существует |
| 58 | + **/ |
| 59 | + public static void createDirIfNotExists(File file) { |
| 60 | + if (!file.exists()) { |
| 61 | + //noinspection ResultOfMethodCallIgnored |
| 62 | + file.mkdirs(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @see FileUtil#getParentDirOfFile(File) |
| 68 | + **/ |
| 69 | + public static void createNewIfNotExist(@NonNull String path) { |
| 70 | + createNewIfNotExist(getFile(path)); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Создать файл, если он существует то ничего не делаем |
| 75 | + **/ |
| 76 | + public static void createNewIfNotExist(@NonNull File file) { |
| 77 | + try { |
| 78 | + if (!file.exists()) { |
| 79 | + createDirIfNotExists(getParentDirOfFile(file)); |
| 80 | + |
| 81 | + //noinspection ResultOfMethodCallIgnored |
| 82 | + file.createNewFile(); |
| 83 | + } |
| 84 | + } catch (Exception e) { |
| 85 | + throw new RuntimeException("Exception while create new file", e); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @see FileUtil#isExist(File) |
| 91 | + **/ |
| 92 | + public static boolean isExist(@NonNull String path) { |
| 93 | + return isExist(getFile(path)); |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Существует ли файл |
| 98 | + **/ |
| 99 | + public static boolean isExist(@NonNull File file) { |
| 100 | + return file.exists(); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @see FileUtil#getFilesList(File) |
| 105 | + **/ |
| 106 | + public static File[] getFilesList(@NonNull String dirPath) { |
| 107 | + return getFilesList(getFile(dirPath)); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Список файлов в дирректории |
| 112 | + **/ |
| 113 | + public static File[] getFilesList(@NonNull File dir) { |
| 114 | + if (!isExist(dir) || !dir.isDirectory()) { |
| 115 | + return null; |
| 116 | + } |
| 117 | + return dir.listFiles(); |
| 118 | + } |
| 119 | + |
| 120 | + public static void delete(@NonNull String path) { |
| 121 | + delete(getFile(path)); |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Если файл, то удаляет |
| 126 | + * Если папка, то удаляет (рекурсивно) |
| 127 | + * #RECURSION |
| 128 | + **/ |
| 129 | + public static void delete(@NonNull File path) { |
| 130 | + if (path.isDirectory()) { |
| 131 | + File[] children = getFilesList(path); |
| 132 | + if (children != null) { |
| 133 | + for (File child : children) { |
| 134 | + delete(child); |
| 135 | + } |
| 136 | + } |
| 137 | + //noinspection ResultOfMethodCallIgnored |
| 138 | + path.delete(); |
| 139 | + |
| 140 | + } else if (path.isFile()) { |
| 141 | + //noinspection ResultOfMethodCallIgnored |
| 142 | + path.delete(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * @see FileUtil#writeText(File, boolean, String) |
| 148 | + **/ |
| 149 | + public static void writeText(@NonNull String path, boolean append, String text) { |
| 150 | + writeText(getFile(path), append, text); |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Записывает текст в файл, если нету то создаст |
| 155 | + **/ |
| 156 | + public static void writeText(@NonNull File file, boolean append, String text) { |
| 157 | + createNewIfNotExist(file); |
| 158 | + try { |
| 159 | + final FileWriter fileWriter = new FileWriter(file, append); |
| 160 | + fileWriter.write(text); |
| 161 | + fileWriter.flush(); |
| 162 | + fileWriter.close(); |
| 163 | + |
| 164 | + } catch (Exception e) { |
| 165 | + throw new RuntimeException("Exception while write text to file", e); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + /** |
| 170 | + * @see FileUtil#setText(File, String) |
| 171 | + **/ |
| 172 | + public static void setText(@NonNull String path, String text) { |
| 173 | + setText(getFile(path), text); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * Ставит в файл текст |
| 178 | + * |
| 179 | + * @see #writeText(File, boolean, String) |
| 180 | + **/ |
| 181 | + public static void setText(@NonNull File file, String text) { |
| 182 | + writeText(file, false, text); |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * @see #addText(File, String) |
| 187 | + **/ |
| 188 | + public static void addText(@NonNull String path, String text) { |
| 189 | + addText(getFile(path), text); |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Добавляет в файл текст в конец |
| 194 | + * |
| 195 | + * @see #writeText(File, boolean, String) |
| 196 | + **/ |
| 197 | + public static void addText(@NonNull File file, String text) { |
| 198 | + writeText(file, true, text); |
| 199 | + } |
| 200 | + |
| 201 | + /** |
| 202 | + * @see #getText(File, String) |
| 203 | + **/ |
| 204 | + public static String getText(@NonNull String path, String defaultValue) { |
| 205 | + return getText(getFile(path), defaultValue); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Получить текст из файла, если файла нету то возвращаем defaultValue |
| 210 | + * если файла не существует, то и не создаём его! |
| 211 | + * |
| 212 | + * @param defaultValue вывод если файла не существует |
| 213 | + **/ |
| 214 | + public static String getText(@NonNull File file, String defaultValue) { |
| 215 | + if (!isExist(file)) { |
| 216 | + return defaultValue; |
| 217 | + } |
| 218 | + |
| 219 | + try { |
| 220 | + final StringBuilder result = new StringBuilder(); |
| 221 | + final FileReader fileReader = new FileReader(file); |
| 222 | + |
| 223 | + final char[] buff = new char[1024]; |
| 224 | + |
| 225 | + int i; |
| 226 | + while ((i = fileReader.read(buff)) > 0) { |
| 227 | + result.append(new String(buff, 0, i)); |
| 228 | + } |
| 229 | + |
| 230 | + fileReader.close(); |
| 231 | + return result.toString(); |
| 232 | + |
| 233 | + } catch (Exception e) { |
| 234 | + throw new RuntimeException("Exception while get file text", e); |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * @see #getText(File) |
| 240 | + **/ |
| 241 | + public static String getText(String path) { |
| 242 | + return getText(getFile(path)); |
| 243 | + } |
| 244 | + |
| 245 | + /** |
| 246 | + * if file not exist: return null |
| 247 | + * |
| 248 | + * @see #getText(String, String) |
| 249 | + **/ |
| 250 | + public static String getText(File file) { |
| 251 | + return getText(file, null); |
| 252 | + } |
| 253 | + |
| 254 | + |
| 255 | + // == DEPRECATED == |
| 256 | + @Deprecated |
| 257 | + public static void write(File file, String content) { |
| 258 | + write(file.getAbsolutePath(), content); |
| 259 | + } |
| 260 | + |
| 261 | + @Deprecated |
| 262 | + public static void write(String path, String content) { |
| 263 | + setText(getFile(path), content); |
| 264 | + } |
| 265 | + |
| 266 | + @Deprecated |
| 267 | + public static String read(File file, String defaultValue) { |
| 268 | + return read(file.getAbsolutePath(), defaultValue); |
| 269 | + } |
| 270 | + |
| 271 | + @Deprecated |
| 272 | + public static String read(String path) { |
| 273 | + return read(path, null); |
| 274 | + } |
| 275 | + |
| 276 | + @Deprecated |
| 277 | + public static String read(String path, String defaultValue) { |
| 278 | + return getText(getFile(path), defaultValue); |
| 279 | + } |
| 280 | + |
| 281 | + @Deprecated |
| 282 | + public static void deleteDir(File dir) { |
| 283 | + delete(dir); |
| 284 | + } |
| 285 | +} |
0 commit comments