convert Stream To UTF-8 String - Android File Input Output

Android examples for File Input Output:Text File

Description

convert Stream To UTF-8 String

Demo Code


//package com.java2s;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

public class Main {
    private static final String ENCODING = "utf-8";

    private static String convertStreamToString(InputStream is)
            throws IOException {
        Writer writer = new StringWriter();

        char[] buffer = new char[2048];
        try {/*from   w  w  w.j  a  v  a  2s  .co m*/
            Reader reader = new BufferedReader(new InputStreamReader(is,
                    ENCODING));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } finally {
            is.close();
        }
        return writer.toString();
    }
}

Related Tutorials