From ec1cda690ea39852f749282528a9e765e5e485d0 Mon Sep 17 00:00:00 2001 From: nathan Date: Sun, 9 Jul 2017 01:39:35 +0200 Subject: [PATCH] Added readLines(File) --- src/dorkbox/util/FileUtil.java | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/dorkbox/util/FileUtil.java b/src/dorkbox/util/FileUtil.java index 12b6121..087e7e3 100644 --- a/src/dorkbox/util/FileUtil.java +++ b/src/dorkbox/util/FileUtil.java @@ -154,19 +154,41 @@ class FileUtil { /** * Reads the contents of the supplied input stream into a list of lines. - * Closes the reader on successful or failed completion. + * + * @return Always returns a list, even if the file does not exist, or there are errors reading it. */ public static - List readLines(Reader in) throws IOException { + List readLines(final File file) { + FileReader fileReader = null; + try { + fileReader = new FileReader(file); + } catch (FileNotFoundException ignored) { + return new ArrayList(); + } + return readLines(fileReader); + } + + /** + * Reads the contents of the supplied input stream into a list of lines. + *

+ * Closes the reader on successful or failed completion. + * + * @return Always returns a list, even if the file does not exist, or there are errors reading it. + */ + public static + List readLines(Reader in) { List lines = new ArrayList(); try { BufferedReader bin = new BufferedReader(in); String line; - while ((line = bin.readLine()) != null) { - lines.add(line); + try { + while ((line = bin.readLine()) != null) { + lines.add(line); + } + } catch (IOException ignored) { } } finally { - IO.close(in); + IO.closeQuietly(in); } return lines; }