我在/ raw文件夹中有一个名为“ max_easy.txt”的txt文件,在此文件中写入了一个数字,在这种情况下为“ 0” ...我想要一个具有0作为Int值的var,我该怎么办那?
我猜这行给我一个String值,我该如何转换它?
InputStream letturaEasy = getResources().openRawResource(R.raw.max_easy);
Run Code Online (Sandbox Code Playgroud)
如果这是您到目前为止所得到的:
InputStream letturaEasy = getResources().openRawResource(R.raw.max_easy);
Run Code Online (Sandbox Code Playgroud)
然后需要做的就是将其转换为String:
String result = getStringFromInputStream(letturaEasy);
Run Code Online (Sandbox Code Playgroud)
最后,到int:
int num = Integer.parseInt(result);
Run Code Online (Sandbox Code Playgroud)
顺便说一下,这里getStringFromInputStream()实现了:
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6872 次 |
| 最近记录: |