简体   繁体   中英

Android Studio: Editing a .txt file in internal storage with EditText

I have a .txt file in the internal storage (the app's data directory) and I have an EditText field that loads up the content in the .txt file.

I want to apply the changes the user makes to that EditText field, and I'm not sure what to do. I tried OutputStreamWriter, but the changes didn't apply.

Any help/guidance is appreciated

This is the code for onClick on the save button:

File file = ....;
editText = (EditText) findViewById(R.id.editText);
String res = editText.getText().toString();
OutputStreamWriter out= new OutputStreamWriter(openFileOutput(file.getName(), Context.MODE_PRIVATE));
out.write(res);
out.close();

the reason I use file.getName() in the openFileOutput parameter is because i got the error with path seperators

please try this code:

File file = ....
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
editText = (EditText) findViewById(R.id.editText);
String res = editText.getText().toString();        
myOutWriter.write(res);
myOutWriter.close();
fOut.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM