使用OutputStreamWriter保存多个字符串

时间:2017-08-18 03:21:41

标签: android storage outputstream

在我的应用中,用户点按屏幕即可获得分数。然后通过该分数,他们可以购买升级。我希望能够保存三个整数值:Counter,add,singleclick。我知道如何保存第一个整数值Counter,但是当我复制并粘贴相同的方法并用添加整数替换Counter整数时它不起作用。你们能告诉我如何保存其他整数值。

请和谢谢

public void writeCounttofile() {
    try {
        OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("countvalue.txt", Context.MODE_PRIVATE));
        outputStreamWriter.write(String.valueOf(Counter));
        outputStreamWriter.close();
    } catch (IOException e) {
        Log.v("MyActivity", e.toString());
    }
}


private  int readCountFromFile() {

    String result = "";
    int countervalue = 0;
    try {

        InputStream inputStream = openFileInput("countvalue.txt");

        if (inputStream != null) {

            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String tempString = "";
            StringBuilder stringBuilder = new StringBuilder();

            while ((tempString = bufferedReader.readLine()) != null) {

                stringBuilder.append(tempString);

            }

            inputStream.close();

            result = stringBuilder.toString();
            countervalue = Integer.parseInt(result);

        }


    } catch (FileNotFoundException e) {
        Log.v("MyActivity", "File not found" + e.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return countervalue;
}

0 个答案:

没有答案