From b8e4f2353ec2c08cb4fdf1708d47180bc9928db6 Mon Sep 17 00:00:00 2001 From: nathan Date: Mon, 29 May 2017 13:47:27 +0200 Subject: [PATCH] Added ability for FileUtil.read() to pass in a string builder, instead of dealing only with strings. Removed line.trim() and isEmpty() checks, since the objective of these methods are to convert a file to a string. Nothing else. --- src/dorkbox/util/FileUtil.java | 58 +++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 19 deletions(-) diff --git a/src/dorkbox/util/FileUtil.java b/src/dorkbox/util/FileUtil.java index 0240ee7..6c4192c 100644 --- a/src/dorkbox/util/FileUtil.java +++ b/src/dorkbox/util/FileUtil.java @@ -86,19 +86,7 @@ class FileUtil { public static byte[] ZIP_HEADER = {'P', 'K', (byte) 0x3, (byte) 0x4}; /** - * Converts the content of a file into a list of strings. - * - * @param file the input file to read. Throws an error if this file cannot be read. - * - * @return A list of strings, one line per string, of the content - */ - public static - List read(final File file) throws IOException { - return read(file, false); - } - - /** - * Converts the content of a file into a list of strings. + * Converts the content of a file into a list of strings. Lines are trimmed. * * @param file the input file to read. Throws an error if this file cannot be read. * @param includeEmptyLines true if you want the resulting list of String to include blank/empty lines from the file @@ -108,10 +96,6 @@ class FileUtil { public static List read(final File file, final boolean includeEmptyLines) throws IOException { List lines = new ArrayList(); - if (!file.canRead()) { - return lines; - } - FileReader fileReader = new FileReader(file); try { BufferedReader bin = new BufferedReader(fileReader); @@ -122,7 +106,7 @@ class FileUtil { lines.add(line); } } else { - while (( line = bin.readLine()) != null) { + while ((line = bin.readLine()) != null) { if (!line.isEmpty()) { lines.add(line); } @@ -134,6 +118,42 @@ class FileUtil { return lines; } + /** + * Convenience method that converts the content of a file into a giant string. + * + * @param file the input file to read. Throws an error if this file cannot be read. + * + * @return A string, matching the contents of the file + */ + public static + String readAsString(final File file) throws IOException { + StringBuilder stringBuilder = new StringBuilder((int) (file.length())); + read(file, stringBuilder); + + return stringBuilder.toString(); + } + + /** + * Writes the content of a file to the passed in StringBuilder. + * + * @param file the input file to read. Throws an error if this file cannot be read. + * @param stringBuilder the stringBuilder this file will be written to + */ + public static + void read(final File file, final StringBuilder stringBuilder) throws IOException { + FileReader fileReader = new FileReader(file); + try { + BufferedReader bin = new BufferedReader(fileReader); + String line; + + while (( line = bin.readLine()) != null) { + stringBuilder.append(line).append(OS.LINE_SEPARATOR); + } + } finally { + IO.closeQuietly(fileReader); + } + } + /** * Reads the contents of the supplied input stream into a list of lines. * Closes the reader on successful or failed completion. @@ -1329,7 +1349,7 @@ class FileUtil { * * (Note the file separator returned will be correct for Windows/Unix) * - * @param file the file to normalize, null returns null + * @param filename the file to normalize, null returns null * @return the normalized file, or null if invalid */ public static