Advertisement
BenTibnam

Save File To Internal Storage in Android (Java)

Nov 11th, 2020
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1. package com.example.simplenotes;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.Bundle;
  6. import android.view.View;
  7. import android.widget.EditText;
  8. import android.widget.Toast;
  9.  
  10. import java.io.BufferedReader;
  11. import java.io.File;
  12. import java.io.FileInputStream;
  13. import java.io.FileNotFoundException;
  14. import java.io.FileOutputStream;
  15. import java.io.IOException;
  16. import java.io.InputStreamReader;
  17.  
  18. public class MainActivity extends AppCompatActivity {
  19.     private static final String FILE_NAME = "example.txt";
  20.     private EditText mEditText;
  21.  
  22.     @Override
  23.     protected void onCreate(Bundle savedInstanceState) {
  24.         super.onCreate(savedInstanceState);
  25.         setContentView(R.layout.activity_main);
  26.  
  27.         mEditText = (EditText) findViewById(R.id.text);
  28.     }
  29.  
  30.     public void save (View view){
  31.         String text = mEditText.getText().toString();
  32.         FileOutputStream fos = null;
  33.  
  34.         try {
  35.             fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
  36.             fos.write(text.getBytes());
  37.             mEditText.getText().clear();
  38.             Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_SHORT).show();
  39.         } catch (FileNotFoundException e) {
  40.             e.printStackTrace();
  41.         } catch (IOException e) {
  42.             e.printStackTrace();
  43.         }finally {
  44.             if(fos != null){
  45.                 try {
  46.                     fos.close();
  47.                 } catch (IOException e) {
  48.                     e.printStackTrace();
  49.                 }
  50.             }
  51.         }
  52.     }
  53.  
  54.     public void load (View view){
  55.         FileInputStream fis = null;
  56.  
  57.         try {
  58.             fis = openFileInput(FILE_NAME);
  59.             InputStreamReader isr = new InputStreamReader(fis);
  60.             BufferedReader br = new BufferedReader(isr);
  61.             StringBuilder sb = new StringBuilder();
  62.             String text;
  63.  
  64.             while((text = br.readLine()) != null){
  65.                 sb.append(text).append('\n');
  66.             }
  67.  
  68.             mEditText.setText(sb.toString());
  69.         } catch (FileNotFoundException e) {
  70.             e.printStackTrace();
  71.         } catch (IOException e) {
  72.             e.printStackTrace();
  73.         }finally {
  74.             if(fis != null){
  75.                 try {
  76.                     fis.close();
  77.                 } catch (IOException e) {
  78.                     e.printStackTrace();
  79.                 }
  80.             }
  81.         }
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement