get Text From Resource - Android File Input Output

Android examples for File Input Output:Text File

Description

get Text From Resource

Demo Code


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import android.content.Context;
import android.os.Environment;

public class Main{
    public static final int DEFAULT_BUFFER_SIZE = 2048;
    public static final String DEFAULT_CHARSET_NAME = "UTF-8";
    public static String getTextFromResource(Context c, int resId) {
        return getTextFromStream(c.getResources().openRawResource(resId),
                true, null);/* w  ww . j  av a2s  .co m*/
    }
    public static String getTextFromStream(InputStream from,
            String charSetName, int bufferSize, boolean closeInput,
            ProgressListener progressListener) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        copyInputStreamToOutputStream(from, bos, bufferSize, closeInput,
                true, progressListener);
        try {
            return bos.toString(charSetName);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static String getTextFromStream(InputStream from,
            boolean closeInput, ProgressListener progressListener) {
        return getTextFromStream(from, DEFAULT_CHARSET_NAME,
                DEFAULT_BUFFER_SIZE, closeInput, progressListener);
    }
    public static void copyInputStreamToOutputStream(InputStream from,
            OutputStream to, int bufferSize, boolean closeInput,
            boolean closeOutput, ProgressListener progressListener) {
        try {
            int totalBytesRead = 0;
            int bytesRead = 0;
            int offset = 0;
            byte[] data = new byte[bufferSize];

            while ((bytesRead = from.read(data, offset, bufferSize)) > 0) {
                totalBytesRead += bytesRead;
                to.write(data, offset, bytesRead);
                if (progressListener != null)
                    progressListener.onProgressUpdate(totalBytesRead);
                //            Log.d(TAG, "Copied " + totalBytesRead + " bytes");
            }
            closeStreams(from, to, closeInput, closeOutput);
        } catch (Exception e) {
            closeStreams(from, to, closeInput, closeOutput);
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    public static void closeStreams(InputStream from, OutputStream to,
            boolean closeInput, boolean closeOutput) {
        try {
            if (to != null)
                to.flush();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (closeInput && from != null)
                from.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            if (closeOutput && to != null)
                to.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials