I've got a json file in R.raw.test123,
I need to process that with GSON.
Step one is; read the text into a string, I want to do that using;
BufferedReader r = new BufferedReader(new FileReader(file));
The FileReader expects a string as filename,开发者_开发问答 but how do I turn R.raw.test123 into a string which I can pass to the FileReader.
I've googled for about 4 hours on this, still can't find it. And I know its probally a noob question, but I'm new to droid programming, I come from a .net background, so this is all very new to me...
Thanks,
Dennis
As answered by hooked82 you can use get the inputstream
with:
InputStream stream = getResources().openRawResource(R.raw.test123);
and then using method to convert it into string:
private static String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
Log.w("LOG", e.getMessage());
} finally {
try {
is.close();
} catch (IOException e) {
Log.w("LOG", e.getMessage());
}
}
return sb.toString();
}
So you can get the string with convertStreamToString(stream);
.
How about doing the following:
InputStream stream = getResources().openRawResource(R.raw.test123);
精彩评论