Java ByteArrayOutputStream Write load(final File file, final String encoding)

Here you can find the source of load(final File file, final String encoding)

Description

load

License

Open Source License

Declaration

public static String load(final File file, final String encoding) throws IOException 

Method Source Code


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

import java.io.BufferedInputStream;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static String load(final File file, final String encoding) throws IOException {

        InputStream is = null;/*from  w  ww .  ja va2s.c  o m*/
        try {
            is = new FileInputStream(file);
            return loadFromStream(is, encoding);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    protected static String loadFromStream(final InputStream is, final String encoding) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        BufferedInputStream bis = null;
        try {
            bis = new BufferedInputStream(is);

            byte[] buffer = new byte[2048];
            int length = 0;
            while ((length = bis.read(buffer)) > 0) {
                baos.write(buffer, 0, length);
            }
        } finally {
            if (bis != null) {
                bis.close();
            }
        }
        return baos.toString(encoding);
    }
}

Related

  1. load(final String resource, final byte[] defaultResult)
  2. load(InputStream in, byte[] buffer, int offset, int initialBufferSize)
  3. load(InputStream inputStream, Class type)
  4. load(InputStream is)