Read and Write Android files and save data

Source: Internet
Author: User
Package com. king. android. db; import java. io. file; import java. io. fileInputStream; import java. io. fileNotFoundException; import java. io. fileOutputStream; import java. io. IOException; import android. app. activity; import android. content. context; import android. OS. bundle; import android. OS. environment; import android. view. view; import android. view. view. onClickListener; import android. widget. button; import android. wi Dget. editText; import com. king. android. r;/*** Description: file storage * by Andy. liu * Time: 08:06:23 **/public class FileStoreActivity extends Activity implements OnClickListener {/** Save the String key */public static final String STRING_KEY = "string_key "; /** Save the String name */public static final String STRING_NAME = "string_name"; public static final String FILE_NAME = "hello.txt"; // save the file name Context mContext = null; editText ed Save, edRead; Button btnSave, btnRead; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); mContext = FileStoreActivity. this; initView ();} private void initView () {setContentView (R. layout. store_layout); edSave = (EditText) findViewById (R. id. ed_edit); edRead = (EditText) findViewById (R. id. ed_show); btnSave = (Button) findViewById (R. id. btn_save); btnSave. setOnClickLi Stener (this); btnRead = (Button) findViewById (R. id. btn_read); btnRead. setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {switch (v. getId () {case R. id. btn_save: filestorgatels. saveFile (edSave. getText (). toString (), filestorgatels. getSDPath () + File. separator + FILE_NAME); break; case R. id. btn_read: edRead. setText (filestorgatels. readFile (filestoremedils. getSDPath () + File. separator + FILE_NAME); break ;}} Final static class filestorgatels {/***** TODO: Save the file root directory * Author: Andy. liu * Create Time: 08:54:14 * TAG: @ return * Return: String */public static String getSDPath () {boolean hasSDCard = Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED); if (hasSDCard) {return Environment. getExternalStorageDirectory (). toString ();} elsereturn Environment. getDownloadCacheDirectory (). toString ();} /***** TODO: Save the file * Author: Andy. liu * Create Time: 08:42:40 * TAG: @ param str file content * TAG: @ param filePath storage path * Return: void */public static void saveFile (String str, string filePath) {FileOutputStream fos = null; try {File file = new File (filePath); if (! File. exists () file. mkdirs (); fos = new FileOutputStream (file); fos. write (str. getBytes (); fos. flush ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (null! = Fos) fos. close ();} catch (IOException e) {e. printStackTrace () ;}}/ ***** TODO: Read the file * Author: Andy. liu * Create Time: 08:48:40 * TAG: @ param filePath * TAG: @ return * Return: String */public static String readFile (String filePath) {FileInputStream FD = null; byte [] mByte = new byte [512]; try {FD = new FileInputStream (new File (filePath); FCM. read (mByte);} catch (FileNotFoundException e) {e. printStack Trace ();} catch (IOException e) {e. printStackTrace ();} finally {try {if (null! = Fiis) FCM. close () ;}catch (IOException e) {e. printStackTrace () ;}return new String (mByte). toString ();}}}
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.