【萬裡征程——Windows App開發】檔案&資料——寫入與讀取,萬裡征程app開發
在前面 【萬裡征程——Windows App開發】檔案&資料——讀取檔案/檔案夾名我們簡單得擷取了檔案名稱和檔案夾名,很明顯沒有太大的意思對吧,這裡就來寫真正的檔案。而在 【萬裡征程——Windows App開發】檔案&資料——檔案選擇器中,已經能夠通過檔案選擇器儲存和開啟檔案了,這裡是對儲存和讀取檔案的一些補充。
準備工作
在XAML中添加一個TextBlock用於顯示相關資訊,添加一個Button來使用它的Click事件,當然了,最後分別建立2個。
建立檔案和讀取檔案
1.執行個體化StorageFolder類
我們的檔案不可能讓其隨意儲存在電腦/手機中的任何一個地方,應該先確定它的檔案夾,對吧?
在新的Windows 8中,微軟開啟了Windows上的App時代,下載的軟體再也不能隨意安裝到任何地方了,而是由作業系統統一放到一塊叫做“隔離儲存區 (Isolated Storage)”的地方。這也是出於安全的考慮。用過Windows Phone 8的朋友應該更加清楚了。
那麼下面這行代碼的LocalFolder究竟在哪裡呢?
StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;
中的檔案,就是我當前所寫的App。(補充一條哦,一開始我裝了Win8後,下載了一個遊戲,類比類的,有金幣呀什麼的,後來我找到這個App的檔案,將資料改了之後金幣就嘩嘩的啦。當然了,對於其他單機而言這個完全不值一提,但App的資料,相信還有很多人沒有改過吧。)
那麼這張圖中的紅方框的檔案夾就是LocalFolder啦,下面還有一個儲存漫遊檔案的檔案夾。
不論是讀取檔案還是寫入檔案,都得先確定一個檔案夾哦。
2.執行個體化StorageFile
確定了檔案夾,就得確定檔案咯。對於建立檔案而言,執行以下代碼。既然用到了非同步,在函數上加上async是必不可少的咯,這一點我們在前面講到過。後面的ReplaceExisting屬性是指的,如果該檔案(名)已經存在了,則替換它。
StorageFile file = await folder.CreateFileAsync("New Document.txt", CreationCollisionOption.ReplaceExisting);
那麼對於讀取檔案呢,就直接讀取好啦。
StorageFile file = await folder.GetFileAsync("sample.txt");
3.建立和讀取檔案
將文本寫入檔案按照如下代碼,將檔案名稱和常值內容(字串)。
await FileIO.WriteTextAsync(file, "Write text to file.");
讀取檔案也是類似的。
string text = await FileIO.ReadTextAsync(file);
我們還可以將這個讀取的字串傳遞給前面定義的TextBlock來加以調試。以下是完整的代碼。
// 建立檔案StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;StorageFile file = await folder.CreateFileAsync("New Document.txt", CreationCollisionOption.ReplaceExisting);await FileIO.WriteTextAsync(file, "Write text to file.");
// 2 從文本讀取檔案StorageFolder folder = Windows.Storage.ApplicationData.Current.LocalFolder;StorageFile file = await folder.GetFileAsync("sample.txt");string text = await Windows.Storage.FileIO.ReadTextAsync(file);tBlockReadInfo.Text = text;
使用緩衝區將位元組寫入到檔案或從檔案讀取位元組
1.執行個體化StorageFolder類
同上。
2.執行個體化StorageFile
同上。
3.將位元組寫入到檔案
a.建立緩衝區
var buffer = Windows.Security.Cryptography.CryptographicBuffer.ConvertStringToBinary("There's buffer ...... ", Windows.Security.Cryptography.BinaryStringEncoding.Utf8);
b.將緩衝區中的位元組寫入到檔案
await Windows.Storage.FileIO.WriteBufferAsync(file, buffer);
4.從檔案讀取位元組
a.將檔案載入到緩衝區
var buffer = await Windows.Storage.FileIO.ReadBufferAsync(file);
b.執行個體化DataReader,讀取緩衝區
DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(buffer);
c.從DataReader對象中讀取字串
string text = dataReader.ReadString(buffer.Length);
使用流將文本寫入檔案或從檔案讀取文本
1.執行個體化StorageFolder類
同上。
2.執行個體化StorageFile
同上。
3.建立流,並非同步地將file開啟,使用可讀寫的方式
var stream = await file.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite);
4.將文本寫入到檔案
a.使用using
using (var writeStream= stream.GetOutputStreamAt(0)){ ......}
b.(在using語句的花括弧內)建立DataWriter對象,並調用DataWriter.WriteString方法,將文本寫入到writeStream中
DataWriter dataWriter = new DataWriter(writeStream);dataWriter.WriteString("Stream is a good thing.");
c.將文本儲存到檔案中,並通過StoreAsync和FlushAsync方法儲存和關閉流
await dataWriter.StoreAsync();await writeStream.FlushAsync();
5.從檔案讀取文本
a.擷取該流的size
var size = stream.Size;
b.使用using
using (var readStream = stream.GetOutputStreamAt(0)){ ......}
c.(在using語句的花括弧內)建立DataWriter對象,並調用LoadAsync方法,最後調用ReadString即可。最後還可以將資訊輸出到TextBlock中。
DataReader dataReader = new DataReader(readStream);uint uintBytes = await dataReader.LoadAsync((uint)size);string text = dataReader.ReadString(uintBytes);tBlockReadInfo.Text = text;