|
8 | 8 | import java.io.FileOutputStream;
|
9 | 9 | import java.io.IOException;
|
10 | 10 | import java.io.InputStreamReader;
|
| 11 | +import java.io.OutputStream; |
| 12 | +import java.nio.charset.Charset; |
| 13 | +import java.nio.charset.StandardCharsets; |
11 | 14 | import java.nio.file.Files;
|
12 | 15 | import java.nio.file.Paths;
|
13 | 16 | import java.util.ArrayList;
|
@@ -146,6 +149,34 @@ public static String readFileToString(File file, String encoding) throws IOExcep
|
146 | 149 | }
|
147 | 150 | }
|
148 | 151 |
|
| 152 | + /** |
| 153 | + * Writes the given data to the given file, creating the file if it does not exist. |
| 154 | + * This method is equivalent to calling {@code writeStringToFile(file, data, StandardCharsets.UTF_8)}. |
| 155 | + * @param file - The file to write to. |
| 156 | + * @param data - The string to write. |
| 157 | + * @throws IOException If an I/O error occurs. |
| 158 | + */ |
| 159 | + public static void writeStringToFile(File file, String data) throws IOException { |
| 160 | + writeStringToFile(file, data, StandardCharsets.UTF_8); |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Writes the given data to the given file, creating the file if it does not exist. |
| 165 | + * @param file - The file to write to. |
| 166 | + * @param data - The string to write. |
| 167 | + * @param charset - The charset used to convert the string to bytes. |
| 168 | + * @throws IOException If an I/O error occurs. |
| 169 | + */ |
| 170 | + public static void writeStringToFile(File file, String data, Charset charset) throws IOException { |
| 171 | + OutputStream out = null; |
| 172 | + try { |
| 173 | + out = new FileOutputStream(file); |
| 174 | + out.write(data.getBytes(charset)); |
| 175 | + } finally { |
| 176 | + IOUtils.closeQuietly(out); |
| 177 | + } |
| 178 | + } |
| 179 | + |
149 | 180 | /**
|
150 | 181 | * Returns true if the given file has any of the given extensions.
|
151 | 182 | *
|
|
0 commit comments