Read and Write Android files and save data
Last Update:2018-12-05
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 ();}}}