Android --- 02 --- saving and reading files

Source: Internet
Author: User

Data storage and access many times the software we develop needs to store the processed data for re-access. Android provides the following methods for data storage: · file · SharedPreferences (parameter) · SQLite database · Content provider · demonstrate file storage and reading in the Network: activity_main.xml [html] <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "fill_parent" android: layout_height = "fill_parent" android: orientation = "vertical"> <TextView android: layout_width = "fill_parent" android: layout _ Height = "wrap_content" android: text = "@ string/text1"/> <EditText android: id = "@ + id/filename" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "abc.txt"/> <TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "file content"/> <EditText android: id = "@ + id/filecontent" android: layout_width = "fill_parent" android: layout_height = "wrap_con Tent "android: minLines =" 3 "/> <Button android: id =" @ + id/myButtonSave "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: text = "save"/> <Button android: id = "@ + id/myButtonRead" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: text = "read"/> </LinearLayout> FileServise. java [java] package com. example. service; import java. io. byteArrayOutputStrea M; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import java. io. outputStream; import java. io. outputStreamWriter; import android. content. context; public class FileServise {/*** save file ** @ param filename * file name * @ param filecontent * file content * @ return */private Context context; public FileServise (Context context) {s Uper (); this. context = context;} public void savefile (String filename, String filecontent) throws IOException {/** IO technology is easy to write from my javase io stream, however, the Context object of android can be used to quickly obtain the output stream. The second parameter is * Private operation mode. The created file can only be accessed by this application. At the same time, files created in private mode will overwrite the content of the source file. */FileOutputStream fos = context. openFileOutput (filename, Context. MODE_PRIVATE); fos. write (filecontent. getBytes (); fos. close ();}/*** File Read ** @ param filename * @ return * @ throws IOException */public String readfile (String filename) throws IOException {FileInputStream FCM = context. openFileInput (filename); // Method for writing to memory ByteArrayOutputStream baos = new ByteArrayOutputStream (); byte [] buffer = New byte [1024]; int len = 0; while (len = Fi. read (buffer ))! =-1) {baos. write (buffer, 0, len);} // obtain byte [] data = baos. toByteArray (); return new String (data );}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.