Jackson_Pike

Read/Write

Nov 18th, 2021
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.00 KB | None | 0 0
  1. private void writeToFile(int value) {
  2.         String text = String.valueOf(value);
  3.  
  4.         try (FileOutputStream fos = context.openFileOutput(classFile, Context.MODE_PRIVATE)) {
  5.             fos.write(text.getBytes());
  6.         } catch (FileNotFoundException e) {
  7.             e.printStackTrace();
  8.         } catch (IOException e) {
  9.             e.printStackTrace();
  10.         }
  11.     }
  12.  
  13.     public int readHighScore() {
  14.         StringBuilder sb = new StringBuilder();
  15.         try (FileInputStream fis = context.openFileInput(classFile)) {
  16.             InputStreamReader isr = new InputStreamReader(fis);
  17.             BufferedReader br = new BufferedReader(isr);
  18.             String text;
  19.  
  20.             while((text = br.readLine()) != null) {
  21.                 sb.append(text);
  22.             }
  23.         } catch (FileNotFoundException e) {
  24.             e.printStackTrace();
  25.         } catch (IOException e) {
  26.             e.printStackTrace();
  27.         }
  28.         return Integer.parseInt(sb.toString());
  29.     }
  30.        
Advertisement
Add Comment
Please, Sign In to add comment