From 9cb481cf17186d46be846e5f7f3de9fbbe3635e9 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Tue, 8 Oct 2013 23:52:19 +0200 Subject: [PATCH] Add methods to FileTools. --- src/fr/devinsy/util/FileTools.java | 134 ++++++++++++++++++++++++++++- 1 file changed, 132 insertions(+), 2 deletions(-) diff --git a/src/fr/devinsy/util/FileTools.java b/src/fr/devinsy/util/FileTools.java index b636679..ad5f1fd 100644 --- a/src/fr/devinsy/util/FileTools.java +++ b/src/fr/devinsy/util/FileTools.java @@ -198,6 +198,22 @@ public class FileTools return result; } + /** + * + * @param file + * @throws IOException + */ + public static StringBuffer loadToStringBuffer(final File source) throws IOException + { + StringBuffer result; + + final String DEFAULT_CHARSET_NAME = "UTF-8"; + result = loadToStringBuffer(source, DEFAULT_CHARSET_NAME); + + // + return result; + } + /** * * @param file @@ -248,6 +264,72 @@ public class FileTools return result; } + /** + * + * @param file + * @throws IOException + */ + public static StringList loadToStringList(final File source) throws IOException + { + StringList result; + + final String DEFAULT_CHARSET_NAME = "UTF-8"; + result = loadToStringList(source, DEFAULT_CHARSET_NAME); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static StringList loadToStringList(final File file, final String charsetName) throws IOException + { + StringList result; + + BufferedReader in = null; + try + { + in = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName)); + + boolean ended = false; + final String LINE_SEPARATOR = System.getProperty("line.separator"); + result = new StringList(); + while (!ended) + { + String line = in.readLine(); + + if (line == null) + { + ended = true; + } + else + { + result.append(line).append(LINE_SEPARATOR); + } + } + } + finally + { + try + { + if (in != null) + { + in.close(); + } + } + catch (IOException exception) + { + exception.printStackTrace(); + } + } + + // + return result; + } + /** * * @param file @@ -299,14 +381,62 @@ public class FileTools * @throws FileNotFoundException * @throws UnsupportedEncodingException */ - public static void save(final File file, final String string) throws UnsupportedEncodingException, FileNotFoundException + public static void save(final File file, final String source) throws UnsupportedEncodingException, FileNotFoundException { PrintWriter out = null; try { out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); - out.println(string); + out.println(source); + } + finally + { + if (out != null) + { + out.close(); + } + } + } + + /** + * + * @param file + * @throws FileNotFoundException + * @throws UnsupportedEncodingException + */ + public static void save(final File file, final StringBuffer source) throws UnsupportedEncodingException, FileNotFoundException + { + PrintWriter out = null; + try + { + out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); + + out.println(source.toString()); + } + finally + { + if (out != null) + { + out.close(); + } + } + } + + /** + * + * @param file + * @throws FileNotFoundException + * @throws UnsupportedEncodingException + */ + public static void save(final File file, final StringList source) throws UnsupportedEncodingException, FileNotFoundException + { + PrintWriter out = null; + try + { + out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); + + out.println(source.toString()); } finally {