2015年12月14日 星期一

Read file As String

public class ReadFile {

    private static String convertStreamToString(InputStream is) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line);//.append("\n");
        }
        reader.close();
        return sb.toString();
    }

    public static String getStringFromFile (String filePath) throws Exception {
        File fl = new File(filePath);
        FileInputStream fin = new FileInputStream(fl);
        String ret = convertStreamToString(fin);
        if (DEBUG) Log.i("nice", "retttttt=" + ret);
        //Make sure you close all streams.
        fin.close();
        return ret;
    }
}

ref : stackoverflow

沒有留言:

張貼留言