title: How to save to File in android programatically
body: <p>I have been searching the internet to find a resolution to my answer, have gone through all the documentation, and have looked through multiple forums before posting my own question on this.  I need to save information through a file because my function</p>
  
  <pre><code>@Override
  protected void onSaveInstanceState(Bundle savedInstanceState) {
      savedInstanceState.putInt("PointCount", pointCount);
      savedInstanceState.putInt("UpOneCost", upOne);
      savedInstanceState.putInt("UpTwoCost", upTwo);
      savedInstanceState.putInt("TimerTime", upgradeTime);
      super.onSaveInstanceState(savedInstanceState);
      Toast.makeText(this, "Saving", Toast.LENGTH_LONG).show();
  }
  </code></pre>
  
  <p>onSaveInstanceState only works at specific intervals at which I'm not entirely clear on.  However, I know it doesn't get called when the activity is destroyed.  The function above is mySaveInstanceState function and below is my restoreInstanceState function</p>
  
  <pre><code>@Override
  protected void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
      Toast.makeText(this, "Restoring", Toast.LENGTH_LONG).show();
      if(savedInstanceState != null){
          pointCount = savedInstanceState.getInt("PointCount");
          upOne = savedInstanceState.getInt("UpOneCost");
          upTwo = savedInstanceState.getInt("UpTwoCost");
          upgradeTime = savedInstanceState.getInt("TimerTime");
          if(upgradeTime &lt; 5500){
              startTimer();
          }
  
          TextView pointCountText = (TextView)findViewById(R.id.myTextView1);
          pointCountText.setText("Points: " + Integer.toString(pointCount));
  
          TextView pointCountText2 = (TextView)findViewById(R.id.upgradeScreenPointText);
          pointCountText2.setText("Points: " + Integer.toString(pointCount));
  
          Button upButtonOne = (Button)findViewById(R.id.upButton1);
          upButtonOne.setText("Cost: " + Integer.toString(upOne));
  
          Button upButtonTwo = (Button)findViewById(R.id.upButton2);
          upButtonTwo.setText("Cost: " + Integer.toString(upTwo));
  
          TextView myTimerValue = (TextView)findViewById(R.id.myTimerValueText);
          myTimerValue.setText("Current Time: " + Integer.toString(upgradeTime));
      }
  }
  </code></pre>
  
  <p>There is a bit of useless info in there but overall this doesn't run when I hit the back button on the android device, so after a bit of research, i decided I should just save it into a file directory.  This is where I created a file in my android project file named "saveFile"  It is a text file, and it is in the same directory as my mainActivity.java file in order to remove file scope complications.  In an effort to reach this file I came to the conclusion of overriding the onDestroy() function and try</p>
  
  <pre><code>String fileName = "saveFile";
  FileOutputStream outputStream;
  
  @Override
  protected void onDestroy() {
      super.onDestroy();
      outputStream.openFileOutput(fileName, Context.MAKE_PRIVATE);
  }
  </code></pre>
  
  <p>At which point I was already stopped because it couldn't find the file.  The documentation is not clear on where this file should go directory wise, and as far as creation and then writing it is also very vague.  If anyone could give a very clear explanation as to how all of it works I would be very appreciative of it.  Thank you =)  Note some of the code may contain minor errors since I typed some of it instead of copy paste but the idea is there.  The only error I receive in the editor is on the outputStream.openFileOutput() line, which says it can't find the file.</p>