Improve method file tools organization.

This commit is contained in:
Christian P. MOMON 2013-08-12 15:16:39 +02:00
parent 7372c65677
commit 18fb286df9
2 changed files with 333 additions and 316 deletions

View file

@ -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;
}
}

View file

@ -1,17 +1,5 @@
package fr.devinsy.util; 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.Arrays;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -30,51 +18,6 @@ import org.apache.commons.lang3.StringUtils;
public class ToolBox 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) public static String clean(final String source)
{ {
String result; String result;
@ -167,39 +110,6 @@ public class ToolBox
return result; 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 * @param pattern
@ -246,232 +156,6 @@ public class ToolBox
return result; 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 * @return