Jackson_Pike icon

Read/Write

Jackson_Pike | PRO | 11/18/21 05:37:32 PM UTC | 0 ⭐ | 1221 👁️ | Never ⏰ | []
Java |

1 KB

|

None

|

0 👍

/

0 👎

private void writeToFile(int value) {
        String text = String.valueOf(value);
 
        try (FileOutputStream fos = context.openFileOutput(classFile, Context.MODE_PRIVATE)) {
            fos.write(text.getBytes());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
    public int readHighScore() {
        StringBuilder sb = new StringBuilder();
        try (FileInputStream fis = context.openFileInput(classFile)) {
            InputStreamReader isr = new InputStreamReader(fis);
            BufferedReader br = new BufferedReader(isr);
            String text;
 
            while((text = br.readLine()) != null) {
                sb.append(text);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Integer.parseInt(sb.toString());
    }
        

Comments