Skip to content

Instantly share code, notes, and snippets.

@jessefreeman
Created January 30, 2011 14:40
Show Gist options
  • Save jessefreeman/802899 to your computer and use it in GitHub Desktop.
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/
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