Skip to content

Commit

Permalink
feat(utils): readFileToString method using UTF-8 charset
Browse files Browse the repository at this point in the history
  • Loading branch information
WakelessSloth56 committed Mar 31, 2022
1 parent 97bb827 commit cce0c0a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/main/java/org/auioc/mods/arnicalib/utils/java/FileUtils.java
Expand Up @@ -3,6 +3,7 @@
import static org.auioc.mods.arnicalib.ArnicaLib.LOGGER;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -37,13 +38,29 @@ public static File getFile(String fileName) throws IOException {
throw new IOException("Could not create parent directory of file \"" + file + "\"");
}

/**
* Reads the contents of a file into a String using the UTF-8 charset.
* The file is always closed.
*
* @param file the file to read, must not be {@code null}
* @return the file contents, never {@code null}
* @throws NullPointerException if file is {@code null}.
* @throws FileNotFoundException if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.
* @throws IOException if an I/O error occurs.
* @see org.apache.commons.io.FileUtils#readFileToString(File, Charset)
* @since 5.1.4
*/
public static String readFileToString(final File file) throws IOException {
return readFileToString(file, Charset.forName("utf8"));
}

/**
* Writes a String to a file creating the file if it does not exist using the UTF-8 charset.
*
* @param file the file to write
* @param data the content to write to the file
* @throws IOException in case of an I/O error
* @see org.apache.commons.io.FileUtils#writeStringToFile(File, String, Charset, boolean)
* @since 5.1.4
*/
public static void writeStringToFile(final File file, final String data) throws IOException {
Expand Down

0 comments on commit cce0c0a

Please sign in to comment.