File storage for Android learning and android file storage
I made a small case of file storage today, and used it to consolidate the unit test and MVC design pattern in Android.
The. AndroidMVC mode is as follows:
<Button android: id = "@ + id/btnSave" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_below = "@ + id/etContent" android: layout_centerHorizontal = "true" android: layout_marginTop = "39dp" android: text = "@ string/btn_tip_save"/> <EditText android: id = "@ + id/etContent" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentTop = "true "Android: layout_centerHorizontal =" true "android: layout_marginTop =" 74dp "android: drawableLeft =" @ drawable/icon "android: EMS =" 15 "android: hint = "@ string/et_tip_writeContent" android: minLines = "3"> <! -- EMS length --> <requestFocus/> </EditText>
Android: EMS = "15" indicates setting the TextView or EditText width to 15 characters.
Step 2: edit the business layer:
The Code is as follows:
Package com. example. savafile. service; import java. io. bufferedReader; import java. io. bufferedWriter; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStreamReader; import java. io. outputStreamWriter; import android. content. context;/** used for page operations. data is stored and read here */public class FileService {private Context context Context; pr Ivate String fileName;/** initialize the Context and file name (PATH) */public FileService (context Context, String fileName) {this. context = context; this. fileName = fileName;} public boolean save (String content) {boolean isSaveSuccessed = false; BufferedWriter bw = null; try {// create an output stream for the Context (Data Flow Outside the Program) // parameter 1 is the stored file name, and parameter 2 is MODE FileOutputStream fos = context. openFileOutput (fileName, Context. MODE_PRIVATE); // construct the byte output stream OutputStre AmWriter writer = new OutputStreamWriter (fos); // wrap the byte output stream as a character output stream bw = new BufferedWriter (writer); // wrap the character output stream as a cache stream? Bw. write (content); // difference between write and OutputStream isSaveSuccessed = true;} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {// write and fos do not need to be closed? If (bw! = Null) {bw. close () ;}} catch (IOException e) {e. printStackTrace () ;}return isSaveSuccessed;} public String read () {String line; StringBuilder sb = new StringBuilder (); // What is the function of BufferedReader br = null; try {FileInputStream FCM = context. openFileInput (fileName); // use the openFileInput (PATH) method in the Context to obtain the input stream br = new BufferedReader (new InputStreamReader (FCM); while (line = br. readLine ())! = Null) {sb. append (line); // s + = line; consistent functions, but will cause a lot of memory waste} catch (IOException e) {// br. readLine () exception Block e. printStackTrace ();} return sb. toString ();}}
The I/O Stream in JAVA is used here, that is, the input/output stream. The following figure shows the relevant information for the small Editor:
Package com. example. savafile. serviceTest; import com. example. savafile. service. fileService; import android. test. androidTestCase;/** unit test */public class ServiceTest extends AndroidTestCase {/** test the save method in FileService */public void testSave () {FileService testSave = new FileService (getContext (), "data"); // is stored as an xml file? TestSave. save ("hello test");}/** test the Read method in FileService */public void testRead () {FileService testRead = new FileService (getContext (), "data "); // is stored as an xml file? TestRead. read ();}}
It is best to name the unit test method name with the test + method to be tested.
③ Run the test:
When a Virtual Machine (or a real machine) is enabled, right-click the outline method in the unit test class and run it as an android project.
Package com. example. savafile; import com. example. savafile. service. fileService; import android. app. activity; import android. OS. bundle; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. widget. editText; import android. widget. toast;/** MVC principles followed by Android development M business layer (FileService) V (xml layout) C control layer (Activity) * test business layer. unit tests must be performed, write the control layer */public class M AinActivity extends Activity implements OnClickListener {private Button btnSvae; private EditText etContent; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); initialize (); readText ();}/** initialize the control and set the corresponding listener */private void initialize () {btnSvae = (Button) findViewById (R. id. btnSave); etContent = (EditText) find ViewById (R. id. etContent); btnSvae. setOnClickListener (this);}/** read data stored earlier */private void readText () {String data = fileService. read (); if (data! = Null) {etContent. setText (data) ;}} FileService fileService = new FileService (MainActivity. this, "data.txt");/** listener Method */@ Override public void onClick (View v) {switch (v. getId () {case R. id. btnSave: if (fileService. save (etContent. getText (). toString () {// whether Toast is successfully saved. makeText (MainActivity. this, "congratulations, saved successfully! ", Toast. LENGTH_SHORT). show ();} else {Toast. makeText (MainActivity. this," it's a pity, saving failed! ", Toast. LENGTH_SHORT). show () ;}break ;}}}
Now, the editing is complete. Thank you.