檔案存取原理與應用.ppt_第1页
檔案存取原理與應用.ppt_第2页
檔案存取原理與應用.ppt_第3页
檔案存取原理與應用.ppt_第4页
檔案存取原理與應用.ppt_第5页
已阅读5页,还剩39页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

第六章,1,檔案存取原理與應用,I/O Processing,實驗目的,學習經由C#基本的IO class能夠對檔案/資料夾進行操作 在PDA上實作檔案總管功能 顯示檔案/資料夾路徑 顯示檔案/資料夾建立、讀取、寫入時間,實驗內容,System.IO namespace,System.IO,FileStream 讀取、寫入、開啟和關閉檔案系統上的檔案 MemoryStream 對記憶體做讀取、寫入動作 StreamReader和StreamWriter 將字元以特定編碼,從 Streams 讀取字元或寫入Streams BinaryReader和BinaryWriter 可對 Streams 當作二進位值讀取和寫入編碼字串,System.IO,Directory和DirectoryInfo 複製、移動、重新命名、建立和刪除目錄 File和FileInfo 複製、移動、重新命名、建立和刪除檔案 Path 提供與檔案或目錄路徑相關的操作 所有Directory和File方法都是static的,不需要事先創造一個物件,System.IO,Streams 將Bytes讀取或寫入檔案中 (例如執行low-level file I/O) Byte-level I/O是利用Stream物件來完成的,Stream (base),FileStream,MemoryStream,System.IO,Readers and writers 在higher level讀取和寫入,例如傳輸value data types, Unicode characters, strings, and lines of text,TextReader (base),StreamReader,StringReader,TextWriter (base),StreamWriter,StringWriter,BinaryReader,BinaryWriter,System.IO,File system 用來操作檔案,像是建立、刪除、找尋、複製,還有維護屬性,FileSystemInfo (base),DirectoryInfo,FileInfo,Directory,File,Path,Three General Categories,Streams(byte-level I/O) Readers and writers File system,Streams(byte-level I/O),Stream物件傳輸bytes資料於儲存裝置的媒介,像是file或是網路socket 因為byte是檔案傳輸最基本的單位,Stream物件提供基本檔案傳輸的能力,不限制於特定的儲存媒介,Streams(byte-level I/O),FileStreams contructor,FileStream fs = new FileStream(string path, FileMode mode); FileStream fs = new FileStream(string path, FileMode mode, FileAccess access); FileStream fs = new FileStream(string path, FileMode mode, FileAccess access, FileShare share);,Streams(byte-level I/O),FileAccess型態用來描述檔案的存取權限, FileMode用來設定開檔的方式,而 FileShare型態用來描述檔案開啟的屬性 FileMode,Streams(byte-level I/O),FileAccess FileShare,Streams(byte-level I/O),FileStream,Encoding class,當資料在傳輸時,它可被encode或是decode。Encoding將資料從Unicode轉換成其他character set。在所有支援能轉換的character set中,都是被應用為用bytes的形式在儲存媒介與stream中傳輸 選擇所想要的Encoding方式,並且指定於reader或是writer的constructor的參數中。當reader或writer物件被建立之後,Encoding的特性將無法被改變,Encoding class,Streams(byte-level I/O),開啟檔案,若是檔案不存在則建立新的檔案,並且寫入新的文字資料,FileStream fs = new FileStream(“.Variables.txt“, FileMode.Append, FileAccess.Write, FileShare.Write); String Line = “This is a new line.“; byte info = Encoding.ASCII.GetBytes(Line); fs.Write(info, 0, info.Length); fs.Close();,Streams(byte-level I/O),開啟檔案並讀取所有文字資料,FileStream f = new FileStream(“.Variables.txt“, FileMode.Open); byte buffer = new byte10000; int count = f.Read(buffer, 0, (int)f.Length); string text = Encoding.ASCII.GetString(buffer, 0, count); f.Close();,Streams(byte-level I/O),Stream-derived classes提供一個一般的介面來對不同的儲存媒介作byte-oriented存取 如果我們面對的是bytes那當然沒有問題,但如果我們需要讀取和寫入two bytes per character的Unicode時該如何? 或許你還可能讀取或寫入integers, floating-point values, or strings in their native mode 因此你可能想要有更highe level的抽象概念,像是讀取或寫入多行文字資料,Three General Categories,Streams(byte-level I/O) Readers and writers File system,Readers and writers,在higher level讀取和寫入,例如傳輸value data types, Unicode characters, strings, and lines of text,Readers and writers,StreamReaders constructor,StreamReader fs = new StreamReader(Stream); StreamReader fs = new StreamReader(String); StreamReader fs = new StreamReader(Stream, Encoding); StreamReader fs = new StreamReader(String, Encoding);,Readers and writers,第一種constructor要傳入Stream的參數,因此先利用FileStream開啟一檔案,再當作參數傳入 第二種constructor較簡單,直接傳入檔案名稱及路徑即可,FileStream f = new FileStream(“.Variables.txt“, FileMode.Open); StreamReader sr = new StreamReader(f),StreamReader sr = new StreamReader(“.Variables.txt“),Readers and writers,StreamReader,Readers and writers,會出現一個messagebox視窗顯示Variables.txt檔案中的所有文字資料,StreamReader sr = new StreamReader(“.Variables.txt“); MessageBox.Show(sr.ReadToEnd(); sr.Close();,Readers and writers,StreamWriters constructor,StreamWriter fs = new StreamWriter(Stream); StreamWriter fs = new StreamWriter(String); StreamWriter fs = new StreamWriter(Stream, Encoding); StreamWriter fs = new StreamWriter(String, Encoding);,Readers and writers,StreamWriter,Readers and writers,將三行文字資料寫入Variables.txt檔案中,StreamWriter sw = new StreamWriter(“.Variables.txt“); sw.WriteLine(“This is“); sw.WriteLine(“a text“) sw.WriteLine(“file“); sw.Close();,實作演練,用StreamReader和StreamWriter的方式重新實作一次同樣的例子,Three General Categories,Streams(byte-level I/O) Readers and writers File system,實驗目的,在PDA上實作檔案總管功能 顯示檔案/資料夾路徑 顯示檔案/資料夾建立、讀取、寫入時間,File system,用來操作檔案,像是建立、刪除、找尋、複製,還有維護屬性 Directory和File classes擁有static methods。不用建立實體物件,就可以經由class reference呼叫static methods,File system,File,File system,Directory,File system,Directory (cont.),實作演練,在檔案總管例子下,新增讀取窗格,點選讀取按鈕後,可讀取文字檔(txt)並顯示文字檔內容於讀取窗格內。 在檔案總管例子下,新增功能列功能-檔案複製、檔案移動、檔案刪除,以加強檔案總管功能。,實驗目的,學習利用PDA提供使用者設定的功能鍵,覆寫自己想要實作的功能,實驗內容,設定PDA功能鍵指定PDA本身提供的程式 將PDA功能鍵和自行撰寫的應用程式做對應 Hardware Button class,PDA設定功能鍵,一般各家PDA廠商生產PDA皆會設置數個不等的功能鍵以對應PDA內建或廠商內附之程式,此功能鍵功能在設定選單中可自行設定 Windows Mobile 6 SDK emulator提供設定Button 2功能鍵,因此實驗之覆寫按鍵為Button 2,PDA設定功能鍵,Hardware Button,在Pocket PC 上設定硬體按鈕功能步驟 建立 HardwareButton 物件 將 AssociatedControl 屬性設定為要啟動的表單或控制項 將 HardwareKey 屬性設為 HardwareKeys之列舉值 硬體按鈕與控制項產生關聯時 按下按鈕,控制項會接收到 Key

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论