|  | 
|  | 1 | +package com.javaquery.util.io; | 
|  | 2 | + | 
|  | 3 | +import com.javaquery.util.Assert; | 
|  | 4 | +import org.slf4j.Logger; | 
|  | 5 | +import org.slf4j.LoggerFactory; | 
|  | 6 | + | 
|  | 7 | +import java.io.BufferedReader; | 
|  | 8 | +import java.io.File; | 
|  | 9 | +import java.io.IOException; | 
|  | 10 | +import java.io.InputStream; | 
|  | 11 | +import java.io.InputStreamReader; | 
|  | 12 | +import java.nio.file.InvalidPathException; | 
|  | 13 | +import java.nio.file.Path; | 
|  | 14 | +import java.nio.file.Paths; | 
|  | 15 | +import java.nio.file.StandardOpenOption; | 
|  | 16 | + | 
|  | 17 | +/** | 
|  | 18 | + * @author vicky.thakor | 
|  | 19 | + * @since 1.0 | 
|  | 20 | + */ | 
|  | 21 | +public final class Files { | 
|  | 22 | + | 
|  | 23 | +    private static final Logger LOGGER = LoggerFactory.getLogger(Files.class); | 
|  | 24 | + | 
|  | 25 | +    /** | 
|  | 26 | +     * Create new, empty file at specified path in {@link File} object. | 
|  | 27 | +     * This method will also creates folder structure if not exists. | 
|  | 28 | +     * | 
|  | 29 | +     * Note: Exception is logged not thrown. | 
|  | 30 | +     * | 
|  | 31 | +     * @param file - file to create | 
|  | 32 | +     * @param <T> | 
|  | 33 | +     * @return <code>true</code> if the named file does not exist and was | 
|  | 34 | +     * successfully created; <code>false</code> if the named file | 
|  | 35 | +     * already exists | 
|  | 36 | +     */ | 
|  | 37 | +    public static <T extends File> boolean createNewFile(T file) { | 
|  | 38 | +        Assert.nonNull(file, NullPointerException::new); | 
|  | 39 | +        if (!file.exists()) { | 
|  | 40 | +            file.getParentFile().mkdirs(); | 
|  | 41 | +            try { | 
|  | 42 | +                return file.createNewFile(); | 
|  | 43 | +            } catch (IOException e) { | 
|  | 44 | +                LOGGER.error(e.getMessage(), e); | 
|  | 45 | +            } | 
|  | 46 | +        } | 
|  | 47 | +        return false; | 
|  | 48 | +    } | 
|  | 49 | + | 
|  | 50 | +    /** | 
|  | 51 | +     * Delete existing file and then Create new, empty file at | 
|  | 52 | +     * specified path in {@link File} object. | 
|  | 53 | +     * | 
|  | 54 | +     * Note: Exception is logged not thrown. | 
|  | 55 | +     * | 
|  | 56 | +     * @param file - file to delete and create | 
|  | 57 | +     * @param <T> | 
|  | 58 | +     * @return <code>true</code> if the named file deleted and was | 
|  | 59 | +     * successfully created; <code>false</code> if the named file | 
|  | 60 | +     * already exists | 
|  | 61 | +     */ | 
|  | 62 | +    public static <T extends File> boolean deleteAndCreateNewFile(T file){ | 
|  | 63 | +        Assert.nonNull(file, NullPointerException::new); | 
|  | 64 | +        if(file.exists()){ | 
|  | 65 | +            file.delete(); | 
|  | 66 | +        } | 
|  | 67 | +        return createNewFile(file); | 
|  | 68 | +    } | 
|  | 69 | + | 
|  | 70 | +    /** | 
|  | 71 | +     * Write data to provided file. | 
|  | 72 | +     * | 
|  | 73 | +     * Note: This method will also creates new <code>file</code> if not exist. | 
|  | 74 | +     * Exception is logged not thrown. | 
|  | 75 | +     * | 
|  | 76 | +     * @param file file to write | 
|  | 77 | +     * @param data data to write to file | 
|  | 78 | +     * @param <T> | 
|  | 79 | +     */ | 
|  | 80 | +    public static <T extends File> void writeToFile(T file, String data){ | 
|  | 81 | +        Assert.nonNull(file, NullPointerException::new); | 
|  | 82 | +        Assert.nonNull(data, NullPointerException::new); | 
|  | 83 | +        if(!file.exists()){ | 
|  | 84 | +            createNewFile(file); | 
|  | 85 | +        } | 
|  | 86 | +        try { | 
|  | 87 | +            java.nio.file.Files.write(getPath(file), data.getBytes()); | 
|  | 88 | +        } catch (IOException | InvalidPathException e) { | 
|  | 89 | +            LOGGER.error(e.getMessage(), e); | 
|  | 90 | +        } | 
|  | 91 | +    } | 
|  | 92 | + | 
|  | 93 | +    /** | 
|  | 94 | +     * Append data to provided <code>file</code> | 
|  | 95 | +     * | 
|  | 96 | +     * Note: This method will also creates new <code>file</code> if not exist. | 
|  | 97 | +     * Exception is logged not thrown. | 
|  | 98 | +     * | 
|  | 99 | +     * @param file file to write | 
|  | 100 | +     * @param data data to append to file | 
|  | 101 | +     * @param appendNewLine <code>true</code> to append new line at the end of data | 
|  | 102 | +     *                      otherwise <code>false</code>. | 
|  | 103 | +     * @param <T> | 
|  | 104 | +     */ | 
|  | 105 | +    public static <T extends File> void appendToFile(T file, String data, boolean appendNewLine){ | 
|  | 106 | +        Assert.nonNull(file, NullPointerException::new); | 
|  | 107 | +        Assert.nonNull(data, NullPointerException::new); | 
|  | 108 | +        if(!file.exists()){ | 
|  | 109 | +            createNewFile(file); | 
|  | 110 | +        } | 
|  | 111 | +        data = appendNewLine ? (data + "\n") : data; | 
|  | 112 | +        try { | 
|  | 113 | +            java.nio.file.Files.write(getPath(file), data.getBytes(), StandardOpenOption.APPEND); | 
|  | 114 | +        } catch (IOException | InvalidPathException e) { | 
|  | 115 | +            LOGGER.error(e.getMessage(), e); | 
|  | 116 | +        } | 
|  | 117 | +    } | 
|  | 118 | + | 
|  | 119 | +    /** | 
|  | 120 | +     * Read <code>String</code> content of <code>file</code>. | 
|  | 121 | +     * | 
|  | 122 | +     * Note: Exception is logged not thrown. | 
|  | 123 | +     * | 
|  | 124 | +     * @param file file to read | 
|  | 125 | +     * @param <T> | 
|  | 126 | +     * @return String data of file if exists otherwise <code>null</code> | 
|  | 127 | +     */ | 
|  | 128 | +    public static <T extends File> String readFromFile(T file){ | 
|  | 129 | +        Assert.nonNull(file, NullPointerException::new); | 
|  | 130 | +        if(file.exists()){ | 
|  | 131 | +            try { | 
|  | 132 | +                return new String(java.nio.file.Files.readAllBytes(getPath(file))); | 
|  | 133 | +            } catch (IOException | InvalidPathException e) { | 
|  | 134 | +                LOGGER.error(e.getMessage(), e); | 
|  | 135 | +            } | 
|  | 136 | +        } | 
|  | 137 | +        return null; | 
|  | 138 | +    } | 
|  | 139 | + | 
|  | 140 | +    /** | 
|  | 141 | +     * Read any file from resources folder of project. | 
|  | 142 | +     * For example, read demo.json from provided path `resources folder` <code>/sample/demo.json</code> | 
|  | 143 | +     * | 
|  | 144 | +     * @param path path to resource | 
|  | 145 | +     * @return String data of file if exists otherwise <code>null</code> | 
|  | 146 | +     */ | 
|  | 147 | +    public String loadResource(String path){ | 
|  | 148 | +        Assert.nonNull(path, NullPointerException::new); | 
|  | 149 | +        if(!path.trim().isEmpty()){ | 
|  | 150 | +            try( | 
|  | 151 | +                InputStream inputStream = getClass().getResourceAsStream(path); | 
|  | 152 | +                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)) | 
|  | 153 | +            ){ | 
|  | 154 | +                String fileLine; | 
|  | 155 | +                StringBuilder stringBuilder = new StringBuilder(); | 
|  | 156 | + | 
|  | 157 | +                while((fileLine = bufferedReader.readLine())!= null){ | 
|  | 158 | +                    stringBuilder.append(fileLine); | 
|  | 159 | +                } | 
|  | 160 | +                return stringBuilder.toString(); | 
|  | 161 | +            } catch (IOException | NullPointerException e) { | 
|  | 162 | +                LOGGER.error(e.getMessage(), e); | 
|  | 163 | +            } | 
|  | 164 | +        } | 
|  | 165 | +        return null; | 
|  | 166 | +    } | 
|  | 167 | + | 
|  | 168 | +    /** | 
|  | 169 | +     * Path of file provided | 
|  | 170 | +     * | 
|  | 171 | +     * @param file file to get {@link Path}. | 
|  | 172 | +     * @param <T> | 
|  | 173 | +     * @return {@link Path} of file | 
|  | 174 | +     */ | 
|  | 175 | +    private static <T extends File> Path getPath(T file){ | 
|  | 176 | +        return Paths.get(file.getAbsolutePath()); | 
|  | 177 | +    } | 
|  | 178 | +} | 
0 commit comments