I had a method like this in an Android app reading a raw file:
public String inputStreamToString(InputStream isTwo) throws IOException {
StringBuffer sBuffer = new StringBuffer();
DataInputStream dataIO = new DataInputStream(isTwo);
String strLineTwo = null;
while ((strLineTwo = dataIO.readLine()) != null) {
sBuffer.append(strLineTwo + "\n");
}
dataIO.close();
isTwo.close();
return sBuffer.toString();
}
However, the DataInputStream object appears to be deprecated now. I researched it and heard it is better to wrap the readline()
with a BufferedInputStream
. Can someone help me finish his (fill in the missing line)? I am not sure how to declare the br
var. This is what I have so far:
public String inputStreamToString(InputStream isTwo) throws IOException {
String strLineTwo = null;
BufferedReader br = null;
StringBuffer sBuffer = new StringBuffer();
InputStreamReader dataIO = new InputStreamReader(isTwo);
while ((strLineTwo = br.readLine()) != null) {
sBuffer.append(strLineTwo + "\n");
}
dataIO.close();
isTwo.close();
return sBuffer.toString();
Here is the preceding code I have not touched yet that calls this method:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tech);
InputStream iFileTwo = getResources().openRawResource(R.raw.testing);
try {
TextView helpText = (TextView) findViewById(R.id.tvStream);
String strFileTwo = inputStreamToString(iFileTwo);
helpText.setText(strFileTwo);
} catch (Exception e) {
Log.e(DEBUG_TAG_THREE, "InputStreamToString failure", e);
}
}
Also, I want to make sure it works from Android 2.3 to 4.2 (current). Thanks for any help.