如何将InputStream转换为int

跳蚤

我在/ raw文件夹中有一个名为“ max_easy.txt”的txt文件,在此文件中写入一个数字,在这种情况下为“ 0” ...我想要一个具有0作为Int值的var,我该怎么办那?

我猜这行给我一个String值,我该如何转换它?

InputStream letturaEasy = getResources().openRawResource(R.raw.max_easy);
卡尔菲利普

如果这是到目前为止,您可以:

InputStream letturaEasy = getResources().openRawResource(R.raw.max_easy);

然后要做的就是将其转换为String

String result = getStringFromInputStream(letturaEasy);

最后是int

int num = Integer.parseInt(result);

顺便说一句,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();     
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章