档案存取原理与应用课件

上传人:痛*** 文档编号:241535450 上传时间:2024-07-02 格式:PPT 页数:44 大小:743KB
返回 下载 相关 举报
档案存取原理与应用课件_第1页
第1页 / 共44页
档案存取原理与应用课件_第2页
第2页 / 共44页
档案存取原理与应用课件_第3页
第3页 / 共44页
点击查看更多>>
资源描述
第六章1檔案存取原理與應用I/O Processing實驗目的學習經由C#基本的IO class能夠對檔案/資料夾進行操作在PDA上實作檔案總管功能顯示檔案/資料夾路徑顯示檔案/資料夾建立、讀取、寫入時間實驗內容System.IO namespaceSystem.IOFileStream讀取、寫入、開啟和關閉檔案系統上的檔案MemoryStream對記憶體做讀取、寫入動作StreamReader和StreamWriter將字元以特定編碼,從 Streams 讀取字元或寫入StreamsBinaryReader和BinaryWriter可對 Streams 當作二進位值讀取和寫入編碼字串System.IODirectory和DirectoryInfo複製、移動、重新命名、建立和刪除目錄File和FileInfo複製、移動、重新命名、建立和刪除檔案Path提供與檔案或目錄路徑相關的操作所有Directory和File方法都是static的,不需要事先創造一個物件System.IOStreams將Bytes讀取或寫入檔案中(例如執行low-level file I/O)Byte-level I/O是利用Stream物件來完成的Stream(base)FileStreamMemoryStreamSystem.IOReaders and writers在higher level讀取和寫入,例如傳輸value data types,Unicode characters,strings,and lines of textTextReader(base)StreamReaderStringReaderTextWriter(base)StreamWriterStringWriterBinaryReaderBinaryWriterSystem.IOFile system用來操作檔案,像是建立、刪除、找尋、複製,還有維護屬性FileSystemInfo(base)DirectoryInfoFileInfoDirectoryFilePathThree General CategoriesStreams(byte-level I/O)Readers and writersFile systemStreams(byte-level I/O)Stream物件傳輸bytes資料於儲存裝置的媒介,像是file或是網路socket因為byte是檔案傳輸最基本的單位,Stream物件提供基本檔案傳輸的能力,不限制於特定的儲存媒介Streams(byte-level I/O)FileStreams contructorFileStream 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型態用來描述檔案開啟的屬性FileModePropertiesDescriptionOpen表示要開啟已存在的檔案CreateNew 表示要產生一個新的檔案Truncate表示要開啟已存在的檔案,並把檔案內容清光Create為CreateNew與Truncate兩者結合Append若檔案不存在則產生新檔案,若檔案存在則寫入動作附加到檔案結尾Streams(byte-level I/O)FileAccessFileSharePropertiesDescriptionNone在檔案開啟中(關閉前)不允許第二次開啟Read允許同時多次開啟,但僅能讀取ReadWrite允許同時多次開啟,可自由讀取及寫入Write允許同時多次開啟,但僅能寫入PropertiesDescriptionRead對檔案僅有讀取的權限ReadWrite對檔案可以自由讀取及寫入Write對檔案僅有寫入的權限Streams(byte-level I/O)FileStreamMethodDescriptionRead(byte,int32,int32)Reads a block of bytes from the stream and writes the data in a given bufferWrite(byte,int32,int32)Writes a block of bytes to this stream using data from a bufferSeek(long offset,SeekOrigin origin)Sets the current position of this stream to the given valueClose()Closes the current stream and releases any resources associated with the current streamEncoding class當資料在傳輸時,它可被encode或是decode。Encoding將資料從Unicode轉換成其他character set。在所有支援能轉換的character set中,都是被應用為用bytes的形式在儲存媒介與stream中傳輸選擇所想要的Encoding方式,並且指定於reader或是writer的constructor的參數中。當reader或writer物件被建立之後,Encoding的特性將無法被改變Encoding classMethodDescriptionConvert(Encoding,Encoding,Byte)Converts a byte array from one encoding to anotherGetBytes(String)When overridden in a derived class,encodes a set of characters into a sequence of bytesGetString(Byte)When overridden in a derived class,decodes a sequence of bytes into a stringASCIIGets an encoding for the ASCII(7-bit)character setUnicodeGets an encoding for the UTF-16 format using the little endian byte orderBigEndianUnicodeGets an encoding for the UTF-16 format using the big endian byte orderUTF7Gets an encoding for the UTF-7 formatUTF8Gets an encoding for the UTF-8 formatStreams(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 CategoriesStreams(byte-level I/O)Readers and writersFile systemReaders and writers在higher level讀取和寫入,例如傳輸value data types,Unicode characters,strings,and lines of textReaders and writersStreamReaders constructorStreamReader 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 writersStreamReaderMethodDescriptionRead()Reads the next character from the input stream and advances the character position by one characterRead(byte,int32,int32)Reads a maximum of count characters from the current stream into buffer,beginning at index.ReadLine()Reads a line of characters from the current stream and returns the data as a stringReadToEnd()Reads the stream from the current position to the end of the streamClose()Closes the StreamReader object and the underlying stream,and releases any system resources associated with the readerReaders and writers會出現一個messagebox視窗顯示Variables.txt檔案中的所有文字資料StreamReader sr=new StreamReader(.Variables.txt);MessageBox.Show(sr.ReadToEnd();sr.Close();Readers and writersStreamWriters constructorStreamWriter fs=new StreamWriter(Stream);StreamWriter fs=new StreamWriter(String);StreamWriter fs=new StreamWriter(Stream,Encoding);StreamWriter fs=new StreamWriter(String,Encoding);Readers and writersStreamWriterMethodDescriptionWrite(String)Writes a string to the streamWrite(byte,int32,int32)Writes a subarray of characters to the streamWriteLine(String)Writes a string followed by a line terminator to the text streamClose()Closes the current StreamWriter object and the underlying streamReaders 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 CategoriesStreams(byte-level I/O)Readers and writersFile system實驗目的在PDA上實作檔案總管功能顯示檔案/資料夾路徑顯示檔案/資料夾建立、讀取、寫入時間File system用來操作檔案,像是建立、刪除、找尋、複製,還有維護屬性Directory和File classes擁有static methods。不用建立實體物件,就可以經由class reference呼叫static methodsFile systemFileMethodDescriptionOpen(String,FileMode)Opens a FileStream on the specified path with read/write accessDelete(String)Overloaded.Deletes a specified directoryExist(String)Determines whether the given path refers to an existing directory on diskMove(String1,String2)Moves a file or a directory and its contents to a new locationGetCreationTime(String)Returns the creation date and time of the specified file or directoryGetLastAccessTime(String)Returns the date and time the specified file or directory was last accessedGetLastWriteTime(String)Returns the date and time the specified file or directory was last written toFile systemDirectoryMethodDescriptionCreateDirectory(String)Overloaded.Creates all the directories in a specified pathCreate(String)Creates or overwrites a file in the specified pathDelete(String)Deletes the specified file.An exception is not thrown if the specified file does not existExist(String)Determines whether the specified file existsMove(String1,String2)Moves a specified file to a new location,providing the option to specify a new file nameGetDirectories(String)Overloaded.Gets the names of subdirectories in a specified directoryGetFiles(String)Overloaded.Returns the names of files in a specified directoryFile systemDirectory(cont.)MethodDescriptionGetCurrentDirectory()Gets the current working directory of the applicationGetDirectoryRoot(String)Returns the volume information,root information,or both for the specified pathMove(String1,String2)Moves a file or a directory and its contents to a new locationGetCreationTime(String)Returns the creation date and time of the specified file or directoryGetLastAccessTime(String)Returns the date and time the specified file or directory was last accessedGetLastWriteTime(String)Returns the date and time the specified file or directory was last written to實作演練在檔案總管例子下,新增讀取窗格,點選讀取按鈕後,可讀取文字檔(txt)並顯示文字檔內容於讀取窗格內。在檔案總管例子下,新增功能列功能-檔案複製、檔案移動、檔案刪除,以加強檔案總管功能。實驗目的學習利用PDA提供使用者設定的功能鍵,覆寫自己想要實作的功能實驗內容設定PDA功能鍵指定PDA本身提供的程式將PDA功能鍵和自行撰寫的應用程式做對應Hardware Button classPDA設定功能鍵一般各家PDA廠商生產PDA皆會設置數個不等的功能鍵以對應PDA內建或廠商內附之程式,此功能鍵功能在設定選單中可自行設定Windows Mobile 6 SDK emulator提供設定Button 2功能鍵,因此實驗之覆寫按鍵為Button 2PDA設定功能鍵Hardware Button在Pocket PC 上設定硬體按鈕功能步驟建立 HardwareButton 物件將 AssociatedControl 屬性設定為要啟動的表單或控制項將 HardwareKey 屬性設為 HardwareKeys之列舉值硬體按鈕與控制項產生關聯時按下按鈕,控制項會接收到 KeyDown 事件,放開按鈕,則會接收到 KeyUp 事件。還原成其原始狀態-AssociatedControl=null硬體按鍵對應KeyEventArgs為鍵盤事件參數其中屬性KeyCode表示按下的鍵值KeyCode定義按鍵鍵值HardwareKeys定義六個硬體按鈕applicationKey1=193applicationKey1=194applicationKey6=197.實作演練使用者在PDA上寫下想要記錄的內容,按下硬體鍵即可將內容儲存為文字檔存在資料夾下
展开阅读全文
相关资源
相关搜索

最新文档


当前位置:首页 > 管理文书 > 施工组织


copyright@ 2023-2025  zhuangpeitu.com 装配图网版权所有   联系电话:18123376007

备案号:ICP2024067431-1 川公网安备51140202000466号


本站为文档C2C交易模式,即用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。装配图网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知装配图网,我们立即给予删除!