Add load file to String methods.

This commit is contained in:
Christian P. MOMON 2013-08-01 00:34:09 +02:00
parent bac4d079c6
commit 931d5f8bb4

View file

@ -1,8 +1,12 @@
package fr.devinsy.util; package fr.devinsy.util;
import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter; import java.io.OutputStreamWriter;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -240,6 +244,76 @@ public class ToolBox
return result; 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 * @param file