Java File Read by Charset load(File file, Charset charset)

Here you can find the source of load(File file, Charset charset)

Description

load

License

Open Source License

Declaration

public static String load(File file, Charset charset) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.*;
import java.nio.charset.Charset;

public class Main {
    private static final int BUFFER_SIZE = 32768;

    public static String load(File file, Charset charset) throws IOException {
        long length = file.length();
        if (length > Integer.MAX_VALUE) {
            throw new UnsupportedOperationException("File too large (" + length + " bytes)");
        }/*  ww w .  j  ava 2 s.co  m*/
        StringBuilder sb = new StringBuilder((int) length);
        try (InputStreamReader in = new InputStreamReader(new BufferedInputStream(new FileInputStream(file)),
                charset)) {
            char[] buffer = new char[BUFFER_SIZE];
            int read;
            while ((read = in.read(buffer)) != -1) {
                sb.append(buffer, 0, read);
            }
        }
        return sb.toString();
    }

    public static String load(InputStream inputStream, Charset charset) throws IOException {
        StringBuilder sb = new StringBuilder();
        try (InputStreamReader in = new InputStreamReader(new BufferedInputStream(inputStream), charset)) {
            char[] buffer = new char[BUFFER_SIZE];
            int read;
            while ((read = in.read(buffer)) != -1) {
                sb.append(buffer, 0, read);
            }
        }
        return sb.toString();
    }
}

Related

  1. getPropertiesVaule(File file, String key, Charset charset)
  2. getString(File file, Charset charset)
  3. getUncommentedLines(File file, Charset forName)
  4. getZipFile(File src, Charset charset)
  5. isEqual(final File document, final Charset a, final Charset b)
  6. loadFile(File file, Charset cs)
  7. loadFile(File logfile, Charset charset)
  8. loadString(File f, Charset charset)
  9. open(File file, Charset charset)