From 931d5f8bb4c8e37fa1da87c71926f742643e8558 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Thu, 1 Aug 2013 00:34:09 +0200 Subject: [PATCH] Add load file to String methods. --- src/fr/devinsy/util/ToolBox.java | 74 ++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/src/fr/devinsy/util/ToolBox.java b/src/fr/devinsy/util/ToolBox.java index fd7f6fa..b2d749f 100644 --- a/src/fr/devinsy/util/ToolBox.java +++ b/src/fr/devinsy/util/ToolBox.java @@ -1,8 +1,12 @@ package fr.devinsy.util; +import java.io.BufferedReader; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; @@ -240,6 +244,76 @@ public class ToolBox return result; } + /** + * + * @param file + * @return + * @throws IOException + */ + static public String load(final File file) throws IOException + { + String result; + + final String DEFAULT_CHARSET_NAME = "UTF-8"; + result = load(file, DEFAULT_CHARSET_NAME); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static String load(final File file, final String charsetName) throws IOException + { + String 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"); + StringBuffer buffer = new StringBuffer((int) file.length()); + while (!ended) + { + String line = in.readLine(); + + if (line == null) + { + ended = true; + } + else + { + buffer.append(line).append(LINE_SEPARATOR); + } + } + + // + result = buffer.toString(); + } + finally + { + try + { + if (in != null) + { + in.close(); + } + } + catch (IOException exception) + { + exception.printStackTrace(); + } + } + + // + return result; + } + /** * * @param file