89 lines
2.7 KiB
Java
89 lines
2.7 KiB
Java
package com.zadanie1.mw;
|
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
import android.app.Activity;
|
|
import android.os.Bundle;
|
|
import android.view.View;
|
|
import android.widget.Button;
|
|
import android.widget.EditText;
|
|
import android.widget.TextView;
|
|
import android.widget.Toast;
|
|
|
|
import java.io.BufferedInputStream;
|
|
import java.io.BufferedReader;
|
|
import java.io.FileInputStream;
|
|
import java.io.FileNotFoundException;
|
|
import java.io.FileOutputStream;
|
|
import java.io.IOException;
|
|
import java.io.InputStream;
|
|
import java.io.InputStreamReader;
|
|
|
|
public class Noto extends AppCompatActivity {
|
|
EditText ChangeNote;
|
|
Button SaveNote;
|
|
|
|
private static final String FILE_NAME = "notes.txt";
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
setContentView(R.layout.activity_noto);
|
|
|
|
ChangeNote = (EditText) findViewById(R.id.changeNote);
|
|
SaveNote = (Button) findViewById(R.id.SaveNote);
|
|
FileInputStream fis = null;
|
|
|
|
try {
|
|
fis = openFileInput(FILE_NAME);
|
|
InputStreamReader isr = new InputStreamReader(fis);
|
|
BufferedReader br = new BufferedReader(isr);
|
|
StringBuffer sb = new StringBuffer();
|
|
String text;
|
|
|
|
while ((text = br.readLine()) != null) {
|
|
sb.append(text).append("\n");
|
|
}
|
|
|
|
if (sb.toString() != null) {
|
|
ChangeNote.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();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public void save (View v) throws IOException {
|
|
|
|
String text = ChangeNote.getText().toString();
|
|
FileOutputStream fos = null;
|
|
|
|
try {
|
|
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
|
|
fos.write(text.getBytes());
|
|
|
|
ChangeNote.getText().clear();
|
|
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME, Toast.LENGTH_LONG).show();
|
|
|
|
} catch (FileNotFoundException e) {
|
|
e.printStackTrace();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
} finally {
|
|
if (fos != null){
|
|
fos.close();
|
|
}
|
|
}
|
|
}
|
|
}
|