1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| public class MainActivity extends ActionBarActivity {
EditText editText;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
textView=(TextView)findViewById(R.id.textView);
}
public void read(View view) {
try {
FileInputStream fileInputStream= openFileInput("myText.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String lines;
while ((lines=bufferedReader.readLine())!=null) {
stringBuffer.append(lines+"\n");
}
textView.setText(stringBuffer.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void write(View view) {
String Mytextmessage = editText.getText().toString();
try {
FileOutputStream fileOutputStream = openFileOutput("myText.txt",MODE_PRIVATE);
fileOutputStream.write(Mytextmessage.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(),"Text Saved",Toast.LENGTH_LONG).show();
editText.setText("");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} |
Partager