get String From InputStream - Android File Input Output

Android examples for File Input Output:InputStream

Description

get String From InputStream

Demo Code


//package com.java2s;

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

public class Main {
    public static String getStringFromInputStream(InputStream is)
            throws IOException {
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int len = -1;

        while ((len = is.read(buffer)) != -1) {
            os.write(buffer, 0, len);/*from   w w w.  j  a v a  2 s .com*/
        }
        is.close();
        String state = os.toString();
        os.close();
        return state;
    }
}

Related Tutorials