From 18fb286df99484905602305fa180d271a3cd3da8 Mon Sep 17 00:00:00 2001 From: "Christian P. MOMON" Date: Mon, 12 Aug 2013 15:16:39 +0200 Subject: [PATCH] Improve method file tools organization. --- src/fr/devinsy/util/FileTools.java | 333 +++++++++++++++++++++++++++++ src/fr/devinsy/util/ToolBox.java | 316 --------------------------- 2 files changed, 333 insertions(+), 316 deletions(-) create mode 100644 src/fr/devinsy/util/FileTools.java diff --git a/src/fr/devinsy/util/FileTools.java b/src/fr/devinsy/util/FileTools.java new file mode 100644 index 0000000..c074a51 --- /dev/null +++ b/src/fr/devinsy/util/FileTools.java @@ -0,0 +1,333 @@ +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.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UnsupportedEncodingException; +import java.net.URL; + + +/** + * + @author christian.momon@devinsy.fr, June 2008, copyright. + * + * This file is free software under the terms of the GNU Library General + * Public License as published by the Free Software Foundation version 3 + * or any later version. + * + */ +public class FileTools +{ + + /** + * + * + * @param file + * Source. + * + * @return Extension value or null. + */ + public static File addToName(final File file, final String addition) + { + File result; + + if (file == null) + { + result = null; + } + else if (addition == null) + { + result = file; + } + else + { + // + String sourceFileName = file.getAbsolutePath(); + int separatorIndex = sourceFileName.lastIndexOf('.'); + + // + String targetFileName; + if (separatorIndex > 0) + { + targetFileName = sourceFileName.substring(0, separatorIndex) + addition + sourceFileName.substring(separatorIndex); + } + else + { + targetFileName = sourceFileName + addition; + } + + // + result = new File(targetFileName); + } + + // + return result; + } + + /** + * Get the extension of a file. + * + * @param file + * Source. + * + * @return Extension value or null. + */ + public static String getExtension(final File file) + { + String result; + + if (file == null) + { + result = null; + } + else + { + int separatorIndex = file.getName().lastIndexOf('.'); + if (separatorIndex > 0) + { + result = file.getName().substring(separatorIndex + 1).toLowerCase(); + } + else + { + result = null; + } + } + + // + return result; + } + + /** + * + * @param file + * @return + * @throws IOException + */ + static public String load(final File source) throws IOException + { + String result; + + final String DEFAULT_CHARSET_NAME = "UTF-8"; + result = load(source, DEFAULT_CHARSET_NAME); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static String load(final File source, final String charsetName) throws IOException + { + String result; + + result = loadToStringBuffer(source, charsetName).toString(); + + // + return result; + } + + /** + * + * @param file + * @return + * @throws IOException + */ + static public String load(final URL source) throws IOException + { + String result; + + final String DEFAULT_CHARSET_NAME = "UTF-8"; + result = load(source, DEFAULT_CHARSET_NAME); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static String load(final URL source, final String charsetName) throws IOException + { + String result; + + // + StringBuffer buffer = new StringBuffer(source.openConnection().getContentLength() + 1); + read(buffer, source.openStream(), charsetName); + + // + result = buffer.toString(); + + // + return result; + } + + /** + * + * @param file + * @throws IOException + */ + public static StringBuffer loadToStringBuffer(final File file, final String charsetName) throws IOException + { + StringBuffer 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 StringBuffer((int) file.length() + 1); + 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 + * @throws IOException + */ + public static void read(final StringBuffer out, final InputStream is, final String charsetName) throws IOException + { + BufferedReader in = null; + try + { + in = new BufferedReader(new InputStreamReader(is, charsetName)); + + boolean ended = false; + final String LINE_SEPARATOR = System.getProperty("line.separator"); + + while (!ended) + { + String line = in.readLine(); + + if (line == null) + { + ended = true; + } + else + { + out.append(line).append(LINE_SEPARATOR); + } + } + } + finally + { + try + { + if (in != null) + { + in.close(); + } + } + catch (IOException exception) + { + exception.printStackTrace(); + } + } + } + + /** + * + * @param file + * @throws FileNotFoundException + * @throws UnsupportedEncodingException + */ + public static void save(final File file, final String string) throws UnsupportedEncodingException, FileNotFoundException + { + PrintWriter out = null; + try + { + out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); + + out.println(string); + } + finally + { + if (out != null) + { + out.close(); + } + } + } + + /** + * + * @param source + * @param extension + * @return + */ + public static File setExtension(final File source, final String extension) + { + File result; + + if ((source == null) || (extension == null)) + { + result = source; + } + else + { + String sourceFileName = source.getAbsolutePath(); + int separatorIndex = sourceFileName.lastIndexOf('.'); + + // + String targetFileName; + if (separatorIndex > 0) + { + targetFileName = sourceFileName.substring(0, separatorIndex) + extension; + } + else + { + targetFileName = sourceFileName + extension; + } + + // + result = new File(targetFileName); + } + + // + return result; + } + +} diff --git a/src/fr/devinsy/util/ToolBox.java b/src/fr/devinsy/util/ToolBox.java index 038b2c2..e1c9fb6 100644 --- a/src/fr/devinsy/util/ToolBox.java +++ b/src/fr/devinsy/util/ToolBox.java @@ -1,17 +1,5 @@ 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.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; -import java.io.PrintWriter; -import java.io.UnsupportedEncodingException; -import java.net.URL; import java.util.Arrays; import java.util.List; import java.util.Set; @@ -30,51 +18,6 @@ import org.apache.commons.lang3.StringUtils; public class ToolBox { - /** - * - * - * @param file - * Source. - * - * @return Extension value or null. - */ - public static File addToName(final File file, final String addition) - { - File result; - - if (file == null) - { - result = null; - } - else if (addition == null) - { - result = file; - } - else - { - // - String sourceFileName = file.getAbsolutePath(); - int separatorIndex = sourceFileName.lastIndexOf('.'); - - // - String targetFileName; - if (separatorIndex > 0) - { - targetFileName = sourceFileName.substring(0, separatorIndex) + addition + sourceFileName.substring(separatorIndex); - } - else - { - targetFileName = sourceFileName + addition; - } - - // - result = new File(targetFileName); - } - - // - return result; - } - public static String clean(final String source) { String result; @@ -167,39 +110,6 @@ public class ToolBox return result; } - /** - * Get the extension of a file. - * - * @param file - * Source. - * - * @return Extension value or null. - */ - public static String getExtension(final File file) - { - String result; - - if (file == null) - { - result = null; - } - else - { - int separatorIndex = file.getName().lastIndexOf('.'); - if (separatorIndex > 0) - { - result = file.getName().substring(separatorIndex + 1).toLowerCase(); - } - else - { - result = null; - } - } - - // - return result; - } - /** * * @param pattern @@ -246,232 +156,6 @@ public class ToolBox return result; } - /** - * - * @param file - * @return - * @throws IOException - */ - static public String load(final File source) throws IOException - { - String result; - - final String DEFAULT_CHARSET_NAME = "UTF-8"; - result = load(source, DEFAULT_CHARSET_NAME); - - // - return result; - } - - /** - * - * @param file - * @throws IOException - */ - public static String load(final File source, final String charsetName) throws IOException - { - String result; - - result = loadToStringBuffer(source, charsetName).toString(); - - // - return result; - } - - /** - * - * @param file - * @return - * @throws IOException - */ - static public String load(final URL source) throws IOException - { - String result; - - final String DEFAULT_CHARSET_NAME = "UTF-8"; - result = load(source, DEFAULT_CHARSET_NAME); - - // - return result; - } - - /** - * - * @param file - * @throws IOException - */ - public static String load(final URL source, final String charsetName) throws IOException - { - String result; - - // - StringBuffer buffer = new StringBuffer(source.openConnection().getContentLength() + 1); - read(buffer, source.openStream(), charsetName); - - // - result = buffer.toString(); - - // - return result; - } - - /** - * - * @param file - * @throws IOException - */ - public static StringBuffer loadToStringBuffer(final File file, final String charsetName) throws IOException - { - StringBuffer 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 StringBuffer((int) file.length() + 1); - 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 - * @throws IOException - */ - public static void read(final StringBuffer out, final InputStream is, final String charsetName) throws IOException - { - BufferedReader in = null; - try - { - in = new BufferedReader(new InputStreamReader(is, charsetName)); - - boolean ended = false; - final String LINE_SEPARATOR = System.getProperty("line.separator"); - - while (!ended) - { - String line = in.readLine(); - - if (line == null) - { - ended = true; - } - else - { - out.append(line).append(LINE_SEPARATOR); - } - } - } - finally - { - try - { - if (in != null) - { - in.close(); - } - } - catch (IOException exception) - { - exception.printStackTrace(); - } - } - } - - /** - * - * @param file - * @throws FileNotFoundException - * @throws UnsupportedEncodingException - */ - public static void save(final File file, final String string) throws UnsupportedEncodingException, FileNotFoundException - { - PrintWriter out = null; - try - { - out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); - - out.println(string); - } - finally - { - if (out != null) - { - out.close(); - } - } - } - - /** - * - * @param source - * @param extension - * @return - */ - public static File setExtension(final File source, final String extension) - { - File result; - - if ((source == null) || (extension == null)) - { - result = source; - } - else - { - String sourceFileName = source.getAbsolutePath(); - int separatorIndex = sourceFileName.lastIndexOf('.'); - - // - String targetFileName; - if (separatorIndex > 0) - { - targetFileName = sourceFileName.substring(0, separatorIndex) + extension; - } - else - { - targetFileName = sourceFileName + extension; - } - - // - result = new File(targetFileName); - } - - // - return result; - } - /** * * @return