|
1 | 1 | package com.example.storage.internal.fouliex.androidinternalstorage;
|
2 | 2 |
|
| 3 | +import android.content.Context; |
3 | 4 | import android.support.v7.app.AppCompatActivity;
|
4 | 5 | import android.os.Bundle;
|
| 6 | +import android.view.View; |
| 7 | +import android.widget.Button; |
| 8 | +import android.widget.EditText; |
| 9 | +import android.widget.Toast; |
| 10 | + |
| 11 | +import java.io.BufferedReader; |
| 12 | +import java.io.FileNotFoundException; |
| 13 | +import java.io.FileOutputStream; |
| 14 | +import java.io.IOException; |
| 15 | +import java.io.InputStreamReader; |
5 | 16 |
|
6 | 17 | public class MainActivity extends AppCompatActivity {
|
| 18 | + EditText editTextFileName, editTextData; |
| 19 | + Button saveButton, readButton; |
7 | 20 |
|
8 | 21 | @Override
|
9 | 22 | protected void onCreate(Bundle savedInstanceState) {
|
10 | 23 | super.onCreate(savedInstanceState);
|
11 | 24 | setContentView(R.layout.activity_main);
|
| 25 | + |
| 26 | + editTextFileName = (EditText) findViewById(R.id.editText1); |
| 27 | + editTextData = (EditText) findViewById(R.id.editText2); |
| 28 | + saveButton = (Button) findViewById(R.id.button1); |
| 29 | + readButton = (Button) findViewById(R.id.button2); |
| 30 | + |
| 31 | + //Performing Action on Read Button |
| 32 | + performActionOnSave(); |
| 33 | + //Performing Action on Save Button |
| 34 | + performActionOnRead(); |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + private void performActionOnRead() { |
| 39 | + readButton.setOnClickListener(new View.OnClickListener() { |
| 40 | + @Override |
| 41 | + public void onClick(View v) { |
| 42 | + String filename = editTextFileName.getText().toString(); |
| 43 | + StringBuffer stringBuffer = new StringBuffer(); |
| 44 | + |
| 45 | + try { |
| 46 | + BufferedReader inputReader = new BufferedReader(new InputStreamReader(openFileInput(filename))); |
| 47 | + String inputString; |
| 48 | + while ((inputString = inputReader.readLine()) != null) { |
| 49 | + stringBuffer.append(inputString + "\n"); |
| 50 | + } |
| 51 | + } catch (FileNotFoundException e) { |
| 52 | + e.printStackTrace(); |
| 53 | + } catch (IOException e) { |
| 54 | + e.printStackTrace(); |
| 55 | + } |
| 56 | + Toast.makeText(getApplicationContext(), stringBuffer.toString(), |
| 57 | + Toast.LENGTH_LONG).show(); |
| 58 | + |
| 59 | + } |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + void performActionOnSave() { |
| 64 | + saveButton.setOnClickListener(new View.OnClickListener() { |
| 65 | + @Override |
| 66 | + public void onClick(View v) { |
| 67 | + String filename = editTextFileName.getText().toString(); |
| 68 | + String data = editTextData.getText().toString(); |
| 69 | + |
| 70 | + FileOutputStream fos; |
| 71 | + |
| 72 | + try { |
| 73 | + fos = openFileOutput(filename, Context.MODE_PRIVATE); |
| 74 | + fos.write(data.getBytes()); |
| 75 | + fos.close(); |
| 76 | + |
| 77 | + Toast.makeText(getApplicationContext(), filename + " saved", Toast.LENGTH_LONG).show(); |
| 78 | + } catch (FileNotFoundException e) { |
| 79 | + e.printStackTrace(); |
| 80 | + } catch (IOException e) { |
| 81 | + e.printStackTrace(); |
| 82 | + } |
| 83 | + } |
| 84 | + }); |
12 | 85 | }
|
13 | 86 | }
|
0 commit comments