




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、第六章,1,檔案存取原理與應用,I/O Processing,實驗目的,學習經由C#基本的IO class能夠對檔案/資料夾進行操作 在PDA上實作檔案總管功能 顯示檔案/資料夾路徑 顯示檔案/資料夾建立、讀取、寫入時間,實驗內容,System.IO namespace,System.IO,FileStream 讀取、寫入、開啟和關閉檔案系統(tǒng)上的檔案 MemoryStream 對記憶體做讀取、寫入動作 StreamReader和StreamWriter 將字元以特定編碼,從 Streams 讀取字元或寫入Streams BinaryReader和BinaryWriter 可對 Streams
2、當作二進位值讀取和寫入編碼字串,System.IO,Directory和DirectoryInfo 複製、移動、重新命名、建立和刪除目錄 File和FileInfo 複製、移動、重新命名、建立和刪除檔案 Path 提供與檔案或目錄路徑相關的操作 所有Directory和File方法都是static的,不需要事先創(chuàng)造一個物件,System.IO,Streams 將Bytes讀取或寫入檔案中 (例如執(zhí)行l(wèi)ow-level file I/O) Byte-level I/O是利用Stream物件來完成的,Stream (base),FileStream,MemoryStream,System.IO,R
3、eaders 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),Directo
4、ryInfo,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
5、 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型態(tài)用來描述檔案的存取權限, FileMode用來設定開檔的方式,而 FileShare型態(tài)用來描述
6、檔案開啟的屬性 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物件
7、被建立之後,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.Lengt
8、h); 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 c
9、lasses提供一個一般的介面來對不同的儲存媒介作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
10、 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, Encodin
11、g); 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
12、(.Variables.txt),Readers and writers,StreamReader,Readers and writers,會出現(xiàn)一個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); S
13、treamWriter 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
14、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擁有st
15、atic methods。不用建立實體物件,就可以經由class reference呼叫static methods,File system,File,File system,Directory,File system,Directory (cont.),實作演練,在檔案總管例子下,新增讀取窗格,點選讀取按鈕後,可讀取文字檔(txt)並顯示文字檔內容於讀取窗格內。 在檔案總管例子下,新增功能列功能-檔案複製、檔案移動、檔案刪除,以加強檔案總管功能。,實驗目的,學習利用PDA提供使用者設定的功能鍵,覆寫自己想要實作的功能,實驗內容,設定PDA功能鍵指定PDA本身提供的程式 將PDA功能鍵和自行撰
16、寫的應用程式做對應 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之列舉值 硬體按鈕與控制項產生關聯(lián)時 按下按鈕,控制項會接收到 KeyD
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 警察執(zhí)法考試試題及答案
- 2025至2030背照式傳感器行業(yè)市場深度研究與戰(zhàn)略咨詢分析報告
- 2025-2030重型叉車行業(yè)市場現(xiàn)狀供需分析及重點企業(yè)投資評估規(guī)劃分析研究報告
- 景區(qū)下半年營銷活動方案
- 木門廠開工活動方案
- 林業(yè)讀書活動方案
- 木屋燒烤活動方案
- 機構活動晚會策劃方案
- 暑假活動游樂園活動方案
- 村居拔河活動方案
- 2025至2030中國礦用卡車行業(yè)發(fā)展趨勢分析與未來投資戰(zhàn)略咨詢研究報告
- 氟骨癥課件教學課件
- 腳手架知識試題集及答案
- 宣城宣州區(qū)“政聘企培”人才引進筆試真題2024
- 診后疾病管理行業(yè)體系構建
- 成都東方廣益投資有限公司下屬企業(yè)招聘筆試真題2024
- 中國鄉(xiāng)村建設運動課件
- 2025至2030年中國高純氮化硅行業(yè)市場全景評估及發(fā)展策略分析報告
- 2024年四川省高校畢業(yè)生“三支一扶”計劃真題
- 2025年農作物種植與管理專業(yè)考試試題及答案
- JG/T 302-2011卷簾門窗
評論
0/150
提交評論