檔案操作之簡單文字檔案
阿新 • • 發佈:2021-01-17
技術標籤:Android
using System; using Android.App; using Android.Content; using Android.Runtime; using Android.Views; using Android.Widget; using Android.OS; using System.IO; using System.Text; using Android.Util; namespace File_QuickEdit { [Activity(Label = "File_QuickEdit", MainLauncher = true , Icon = "@drawable/icon")] public class QuickEdit : Activity { const int MENU_GROUP = 0; const int SAVE_FILE_MENU_ID = 1; const int SAVE_FILE_MENU_ORDER = 1; const string QUICKEDIT_TAG = "QUICKEDIT"; const string QUICKEDIT_FILENAME = "quickedit.txt"; protected override void OnCreate( Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView( Resource.Layout .Main); OpenFileInput(); } /// <summary> /// 建立選項選單 /// </summary> /// <param name=" menu"></param> /// <returns></returns> public override bool OnCreateOptionsMenu( IMenu menu) { base.OnCreateOptionsMenu(menu); menu.Add(MENU_GROUP, SAVE_FILE_MENU_ID, SAVE_FILE_MENU_ORDER, Resource.String .Save); return true ; } public override bool OnOptionsItemSelected( IMenuItem item) { base.OnOptionsItemSelected(item); switch(item.ItemId) { case SAVE_FILE_MENU_ID: SaveFile(); return true ; } return false ; } private void SaveFile() { EditText editText = (EditText )FindViewById(Resource. Id.fileEditor); String content = editText.Text; try { Stream fos = OpenFileOutput(QUICKEDIT_FILENAME, FileCreationMode.Private); UTF8Encoding enc = new UTF8Encoding(); fos.Write(enc.GetBytes(content), 0, enc.GetBytes(content).Length); fos.Close(); } catch(Java.IO.FileNotFoundException e) { Log.Error(QUICKEDIT_TAG, e.Message); } catch (Java.IO.IOException e) { Log.Error(QUICKEDIT_TAG, e.Message); } } void SaveExternalFile() { EditText editText = (EditText )FindViewById(Resource. Id.fileEditor); String content = editText.Text.ToString(); try { if(Android.OS.Environment .ExternalStorageState == Android.OS.Environment.MediaMounted) { Java.IO. File dataDir = this .GetExternalFilesDir(Android.OS.Environment.DataDirectory.Path); Stream fos = OpenFileOutput(dataDir+QUICKEDIT_FILENAME, FileCreationMode.Private); UTF8Encoding enc = new UTF8Encoding(); fos.Write(enc.GetBytes(content), 0, enc.GetBytes(content).Length); fos.Close(); } } catch (Java.IO.FileNotFoundException e) { Log.Error(QUICKEDIT_TAG, e.Message); } catch (Java.IO.IOException e) { Log.Error(QUICKEDIT_TAG, e.Message); } } private void OpenFileInput() { byte[] content = new byte[1024]; try { Stream fis = OpenFileInput(QUICKEDIT_FILENAME); if((fis.Read(content,0,content.Length))>0) { EditText editText = (EditText )FindViewById(Resource. Id.fileEditor); UTF8Encoding enc = new UTF8Encoding(); char[] chars = enc.GetChars(content); int charLength = chars.Length; if(charLength>0) { editText.SetText( new string (chars), Android.Widget.TextView. BufferType.Editable); } } } catch (Java.IO.FileNotFoundException e) { Log.Error(QUICKEDIT_TAG, e.Message); } catch (Java.IO.IOException e) { Log.Error(QUICKEDIT_TAG, e.Message); } } } }