Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.simplenotes;
- import androidx.appcompat.app.AppCompatActivity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.EditText;
- import android.widget.Toast;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class MainActivity extends AppCompatActivity {
- private static final String FILE_NAME = "example.txt";
- private EditText mEditText;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mEditText = (EditText) findViewById(R.id.text);
- }
- public void save (View view){
- String text = mEditText.getText().toString();
- FileOutputStream fos = null;
- try {
- fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
- fos.write(text.getBytes());
- mEditText.getText().clear();
- Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_SHORT).show();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(fos != null){
- try {
- fos.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- public void load (View view){
- FileInputStream fis = null;
- try {
- fis = openFileInput(FILE_NAME);
- InputStreamReader isr = new InputStreamReader(fis);
- BufferedReader br = new BufferedReader(isr);
- StringBuilder sb = new StringBuilder();
- String text;
- while((text = br.readLine()) != null){
- sb.append(text).append('\n');
- }
- mEditText.setText(sb.toString());
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally {
- if(fis != null){
- try {
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement