How to convert InputStream to int
This question already has an answer here:
If this is what you got so far:
InputStream letturaEasy = getResources().openRawResource(R.raw.max_easy);
Then all that needs to be done is to convert that to a String :
String result = getStringFromInputStream(letturaEasy);
And finally, to int :
int num = Integer.parseInt(result);
By the way, getStringFromInputStream()
was implemented here:
private static String getStringFromInputStream(InputStream is) {
BufferedReader br = null;
StringBuilder sb = new StringBuilder();
String line;
try {
br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return sb.toString();
}
You can use BufferedReader
to read lines as strings from that file. Integer.parseInt
will parse them to ints:
try(BufferedReader reader = new BufferedReader(new InputStreamReader(letturaEasy, "UTF8")) ) {
int n = Integer.parseInt(reader.readLine());
}
链接地址: http://www.djcxy.com/p/20920.html
上一篇: 方法将字符串转换为int
下一篇: 如何将InputStream转换为int