Add methods to FileTools.

This commit is contained in:
Christian P. MOMON 2013-10-08 23:52:19 +02:00
parent 44d2565826
commit 9cb481cf17

View file

@ -198,6 +198,22 @@ public class FileTools
return result; 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 * @param file
@ -248,6 +264,72 @@ public class FileTools
return result; 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 * @param file
@ -299,14 +381,62 @@ public class FileTools
* @throws FileNotFoundException * @throws FileNotFoundException
* @throws UnsupportedEncodingException * @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; PrintWriter out = null;
try try
{ {
out = new PrintWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8")); 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 finally
{ {