File read/write using the android sub-function
Create an Android project.
1 Layout
First look:
Open main. xml and modify the content as follows:
Binary definition string
Open strings. xml and add the following content:
File
Settings
File Name
File Content
Save
Saved
Failed to save
Read
Read successful
Reading failed
Three Functions
Create a new FileService class for reading and writing files. The complete code is as follows:
Public class FileService {public Context context; public FileService (Context context) {this. context = context;}/*** save the String to the file * @ param name file name * @ param content File content */public void save (String name, String content) throws Exception {// The File Created by MODE_PRIVATE can only be accessed by this application, and the newly written content will overwrite the original content FileOutputStream OS = context. openFileOutput (name, Context. MODE_PRIVATE); // It is stored in/data/by default/
/Files directory OS. write (content. getBytes (); OS. close ();}/***** read File Content * @ param name file name * @ return * @ throws Exception */public String read (String name) throws Exception {FileInputStream is = context. openFileInput (name); ByteArrayOutputStream OS = new ByteArrayOutputStream (); byte [] buf = new byte [1024]; int len = 0; while (len = is. read (buf ))! =-1) {OS. write (buf, 0, len);} byte [] data = OS. toByteArray (); String content = new String (data); return content ;}}
4. Test code
The code for modifying MainActivity. java is as follows:
public EditText nameText; public EditText saveContentText; public EditText readContentText; public Button button_read; public String filename; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); nameText = (EditText)this.findViewById(R.id.filename); saveContentText = (EditText)this.findViewById(R.id.save_file_content); readContentText = (EditText)this.findViewById(R.id.read_file_content); Button button_save = (Button) this.findViewById(R.id.button_save); button_save.setOnClickListener(new ButtonSaveClickListener()); button_read = (Button) this.findViewById(R.id.button_read); button_read.setClickable(false); button_read.setOnClickListener(new ButtonReadClickListener()); } private final class ButtonSaveClickListener implements View.OnClickListener{ public void onClick(View v){ String name = nameText.getText().toString(); String content = saveContentText.getText().toString(); FileService service = new FileService(getApplicationContext()); try { filename = name; service.save(name, content); filename = name; button_read.setClickable(true); Toast.makeText(getApplicationContext(),R.string.save_success, Toast.LENGTH_LONG).show(); }catch (Exception e){ Toast.makeText(getApplicationContext(),R.string.save_fail, Toast.LENGTH_LONG).show(); e.printStackTrace(); } } } private final class ButtonReadClickListener implements View.OnClickListener{ public void onClick(View v){ FileService service = new FileService(getApplicationContext()); try { String content = service.read(filename); readContentText.setText(content); Toast.makeText(getApplicationContext(),R.string.read_success, Toast.LENGTH_LONG).show(); }catch (Exception e){ Toast.makeText(getApplicationContext(),R.string.read_fail, Toast.LENGTH_LONG).show(); e.printStackTrace(); } } }
Running result