Created
January 30, 2011 14:40
-
-
Save jessefreeman/802899 to your computer and use it in GitHub Desktop.
A simple util to help convert an InputStream into a String. Useful for parsing JSON files from the asset folder. Based on an example at http://thedevelopersinfo.com/2009/11/17/using-assets-in-android/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.gamecook.cigarsmuggler.utils; | |
import java.io.ByteArrayOutputStream; | |
import java.io.IOException; | |
import java.io.InputStream; | |
/** | |
* User: Jesse Freeman | |
* Date: 1/30/11 | |
* Time: 9:11 AM | |
*/ | |
public class TextFileUtil | |
{ | |
/** | |
* This method reads simple text file | |
* @param inputStream | |
* @return data from file | |
*/ | |
public static String readTextFile(InputStream inputStream) { | |
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | |
byte buf[] = new byte[1024]; | |
int len; | |
try { | |
while ((len = inputStream.read(buf)) != -1) { | |
outputStream.write(buf, 0, len); | |
} | |
outputStream.close(); | |
inputStream.close(); | |
} catch (IOException e) { | |
} | |
return outputStream.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment