Cache Storage

MainActivity.java
 public class MainActivity extends AppCompatActivity {
  String FileName = "myFile";
  Button BtnSave, BtnRead;
  EditText editName;
  TextView readName;
  String name;
  @Override 
   protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);
   BtnSave = (Button) findViewById(R.id.idBtnSave);
   BtnSave.setOnClickListener(new View.OnClickListener() {
    @Override 
     public void onClick(View v) {
     saveFile();
   }
  });
   BtnRead = (Button) findViewById(R.id.idBtnRead);
   BtnRead.setOnClickListener(new View.OnClickListener() {
   @Override 
    public void onClick(View v) {
     readFile();
   }
  });
   editName = (EditText) findViewById(R.id.idName);
   readName = (TextView) findViewById(R.id.idReadtxt);
 }
 private void readFile() {
  
  try {
    File file = new File(getCacheDir(), FileName);
    FileInputStream fin = new FileInputStream(file);
    InputStreamReader inputStream = new InputStreamReader(fin);
    BufferedReader bufferedReader = new BufferedReader(inputStream);
    StringBuilder stringBuilder = new StringBuilder();
    String line = null;
   while ((line = bufferedReader.readLine()) != null) {
     stringBuilder.append(line);
    
   }
    fin.close();
    inputStream.close();
    readName.setText("Name: " + stringBuilder.toString());
    Toast.makeText(this, "Data Retrieved :" + stringBuilder.toString(), Toast.LENGTH_SHORT).show();
   
  } catch (java.io.IOException e) {
    e.printStackTrace();
  }
 }
 private void saveFile() {
  try {
    //To append the Data in the File use Context.MODE_APPEND 
    File file = new File(getCacheDir(), FileName);
    FileOutputStream fos = new FileOutputStream(file);
    name = editName.getText().toString();
    fos.write(name.getBytes());
    fos.close();
    Toast.makeText(this, "Data Saved", Toast.LENGTH_SHORT).show();
   
  } catch (java.io.IOException e) {
    e.printStackTrace();
  }
 }
}

activity_main.xml
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_main" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     android:orientation="vertical" 
     tools:context="com.example.arjun.storage.MainActivity"> 
    
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"
     android:inputType="textPersonName"
     android:hint="Name:" 
     android:ems="10"
     android:id="@+id/idName" /> 
    
    <LinearLayout 
     android:layout_width="match_parent"
     android:layout_height="wrap_content" 
     android:orientation="horizontal">
    
        <Button 
         android:text="Save To File" 
         android:layout_width="match_parent"
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:layout_margin="5dp"
         android:id="@+id/idBtnSave" />
        
        <Button 
         android:text="Read File" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_weight="1" 
         android:layout_margin="5dp"
         android:id="@+id/idBtnRead" /> 
        
    </LinearLayout>  
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content"
     android:textSize="20sp" 
     android:textColor="#000000" 
     android:id="@+id/idReadtxt"/>  
</LinearLayout>  

Share this