Java Utililty Methods File Read by Charset

List of utility methods to do File Read by Charset

Description

The list of methods to do File Read by Charset are organized into topic(s).

Method

ZipFilegetZipFile(File src, Charset charset)
Returns a zipFile opened with a given charset
if (charset == null) {
    return new ZipFile(src);
try {
    Constructor<ZipFile> constructor = ZipFile.class
            .getConstructor(new Class[] { File.class, Charset.class });
    return (ZipFile) constructor.newInstance(new Object[] { src, charset });
} catch (NoSuchMethodException e) {
...
booleanisEqual(final File document, final Charset a, final Charset b)
Tests, whether the content of the given file is identical at character level, when it is opened with both different Charsets.
boolean ret = true;
FileInputStream aIn = null;
FileInputStream bIn = null;
InputStreamReader aReader = null;
InputStreamReader bReader = null;
try {
    aIn = new FileInputStream(document);
    bIn = new FileInputStream(document);
...
Stringload(File file, Charset charset)
load
long length = file.length();
if (length > Integer.MAX_VALUE) {
    throw new UnsupportedOperationException("File too large (" + length + " bytes)");
StringBuilder sb = new StringBuilder((int) length);
try (InputStreamReader in = new InputStreamReader(new BufferedInputStream(new FileInputStream(file)),
        charset)) {
    char[] buffer = new char[BUFFER_SIZE];
...
StringloadFile(File file, Charset cs)
Load a file with a given charset into a buffer
InputStream in;
in = new FileInputStream(file);
return loadInputStream(in, cs);
StringloadFile(File logfile, Charset charset)
load File
if (!logfile.exists()) {
    return "";
StringBuilder str = new StringBuilder((int) logfile.length());
BufferedReader r = new BufferedReader(new InputStreamReader(new FileInputStream(logfile), charset));
char[] buf = new char[1024];
int len;
while ((len = r.read(buf, 0, buf.length)) > 0) {
...
StringloadString(File f, Charset charset)
load String
FileInputStream in = new FileInputStream(f);
try {
    return loadString(in, charset);
} finally {
    in.close();
BufferedReaderopen(File file, Charset charset)
open
InputStream in = new FileInputStream(file);
return new BufferedReader(new InputStreamReader(in, charset));
MapparseNameDelimiterValueNewLineFile(File nameDelimiterValueNewLineFile, String nameValueDelimiter, Charset charset)
parse Name Delimiter Value New Line File
Map<String, String> parsedNameValues = new HashMap<String, String>();
InputStream fileInputStream = new FileInputStream(nameDelimiterValueNewLineFile);
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream, charset))) {
    String currentRow = null;
    while ((currentRow = bufferedReader.readLine()) != null) {
        String[] rowValues = currentRow.split(nameValueDelimiter);
        if (rowValues.length > 1) {
            parsedNameValues.put(rowValues[0], rowValues[1]);
...
voidsaveFile(File file, String content, String charsetName)
save File
file.getParentFile().mkdirs();
Charset cs;
if (null == charsetName || "".equals(charsetName)) {
    cs = Charset.defaultCharset();
} else {
    cs = Charset.forName(charsetName);
CharsetEncoder encoder = cs.newEncoder();
...
voidsaveToFile(File dest, String contents, Charset cs)
Save text to a file
OutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
Writer writer = new OutputStreamWriter(out, cs);
try {
    writer.write(contents);
} finally {
    writer.close();