背水一戰 Windows 10 (89) - 文件系統: 讀寫文本數據, 讀寫二進制數據, 讀寫流數據
作者:webabcd
介紹
背水一戰 Windows 10 之 文件系統
- 讀寫文本數據
- 讀寫二進制數據
- 讀寫流數據
示例
1、演示如何讀寫文本數據
FileSystem/ReadWriteText.xaml
<Page x:Class="Windows10.FileSystem.ReadWriteText" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.FileSystem" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnWriteText" Content="Write Text" Click="btnWriteText_Click" Margin="5" /> <Button Name="btnReadText" Content="Read Text" Click="btnReadText_Click" Margin="5" /> </StackPanel> </Grid> </Page>
FileSystem/ReadWriteText.xaml.cs
/* * 演示如何讀寫文本數據 * * FileIO - 用於讀寫 IStorageFile 對象的幫助類 * WriteTextAsync() - 將指定的文本數據寫入到指定的文件 * AppendTextAsync() - 將指定的文本數據追加到指定的文件 * WriteLinesAsync() - 將指定的多段文本數據寫入到指定的文件 * AppendLinesAsync() - 將指定的多段文本數據追加到指定的文件 * ReadTextAsync() - 獲取指定的文件中的文本數據 * ReadLinesAsync() - 獲取指定的文件中的文本數據,返回的是一行一行的數據 */ using System; using Windows.Storage; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.FileSystem { public sealed partial class ReadWriteText : Page { public ReadWriteText() { this.InitializeComponent(); } private async void btnWriteText_Click(object sender, RoutedEventArgs e) { // 在指定的目錄下創建指定的文件 StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary); StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdText.txt", CreationCollisionOption.ReplaceExisting); // 在指定的文件中寫入指定的文本 string textContent = "I am webabcd"; await FileIO.WriteTextAsync(storageFile, textContent, Windows.Storage.Streams.UnicodeEncoding.Utf8); // 編碼為 UnicodeEncoding.Utf8 lblMsg.Text = "寫入成功"; } private async void btnReadText_Click(object sender, RoutedEventArgs e) { // 在指定的目錄下獲取指定的文件 StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary); StorageFile storageFile = await storageFolder.GetFileAsync("webabcdText.txt"); if (storageFile != null) { // 獲取指定的文件中的文本內容 string textContent = await FileIO.ReadTextAsync(storageFile, Windows.Storage.Streams.UnicodeEncoding.Utf8); // 編碼為 UnicodeEncoding.Utf8 lblMsg.Text = "讀取結果:" + textContent; } } } }
2、演示如何讀寫二進制數據
FileSystem/ReadWriteBinary.xaml
<Page x:Class="Windows10.FileSystem.ReadWriteBinary" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.FileSystem" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnWriteBinary" Content="Write Binary" Click="btnWriteBinary_Click" Margin="5" /> <Button Name="btnReadBinary" Content="Read Binary" Click="btnReadBinary_Click" Margin="5" /> </StackPanel> </Grid> </Page>
FileSystem/ReadWriteBinary.xaml.cs
/* * 演示如何讀寫二進制數據 * * FileIO - 用於讀寫 IStorageFile 對象的幫助類 * WriteBufferAsync() - 將指定的二進制數據寫入指定的文件 * ReadBufferAsync() - 獲取指定的文件中的二進制數據 * * IBuffer - 字節數組 */ using System; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.FileSystem { public sealed partial class ReadWriteBinary : Page { public ReadWriteBinary() { this.InitializeComponent(); } private async void btnWriteBinary_Click(object sender, RoutedEventArgs e) { // 在指定的目錄下創建指定的文件 StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary); StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdBinary.txt", CreationCollisionOption.ReplaceExisting); // 將字符串轉換成二進制數據,並保存到指定文件 string textContent = "I am webabcd"; IBuffer buffer = ConverterHelper.String2Buffer(textContent); await FileIO.WriteBufferAsync(storageFile, buffer); lblMsg.Text = "寫入成功"; } private async void btnReadBinary_Click(object sender, RoutedEventArgs e) { // 在指定的目錄下獲取指定的文件 StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary); StorageFile storageFile = await storageFolder.GetFileAsync("webabcdBinary.txt"); if (storageFile != null) { // 獲取指定文件中的二進制數據,將其轉換成字符串並顯示 IBuffer buffer = await FileIO.ReadBufferAsync(storageFile); string textContent = ConverterHelper.Buffer2String(buffer); lblMsg.Text = "讀取結果:" + textContent; } } } }
3、演示如何讀寫流數據
FileSystem/ReadWriteStream.xaml
<Page x:Class="Windows10.FileSystem.ReadWriteStream" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:Windows10.FileSystem" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="10 0 10 10"> <TextBlock Name="lblMsg" Margin="5" /> <Button Name="btnWriteStream1" Content="Write Stream(通過 IRandomAccessStream 寫)" Click="btnWriteStream1_Click" Margin="5" /> <Button Name="btnWriteStream2" Content="Write Stream(通過 StorageStreamTransaction 寫)" Click="btnWriteStream2_Click" Margin="5" /> <Button Name="btnReadStream" Content="Read Stream" Click="btnReadStream_Click" Margin="5" /> </StackPanel> </Grid> </Page>
FileSystem/ReadWriteStream.xaml.cs
/* * 演示如何讀寫流數據 * * IBuffer - 字節數組 * * IInputStream - 支持讀取的流 * IOutputStream - 支持寫入的流 * IRandomAccessStream - 支持讀取和寫入的流,其繼承自 IInputStream 和 IOutputStream * * DataReader - 數據讀取器,用於從數據流中讀取數據 * LoadAsync() - 從數據流中加載指定長度的數據到緩沖區 * ReadInt32(), ReadByte(), ReadString() 等 - 從緩沖區中讀取數據 * DataWriter - 數據寫入器,用於將數據寫入數據流 * WriteInt32(), WriteByte(), WriteString() 等 - 將數據寫入緩沖區 * StoreAsync() - 將緩沖區中的數據保存到數據流 * * StorageStreamTransaction - 用於寫數據流到文件的類(它寫文件的方式是:先寫臨時文件,然后臨時文件重命名) * Stream - 數據流(只讀) * CommitAsync - 重命名臨時文件 * * StorageFile - 文件操作類 * public IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode) - 打開文件,返回 IRandomAccessStream 對象 * public IAsyncOperation<IRandomAccessStream> OpenAsync(FileAccessMode accessMode, StorageOpenOptions options) - 打開文件,返回 IRandomAccessStream 對象 * FileAccessMode.Read - 返回的流可讀,不可寫 * FileAccessMode.ReadWrite - 返回的流可讀,可寫 * StorageOpenOptions.AllowOnlyReaders - 其他調用者可讀不可寫 * StorageOpenOptions.AllowReadersAndWriters - 其他調用者可讀可寫 * public IAsyncOperation<StorageStreamTransaction> OpenTransactedWriteAsync() - 打開文件,返回 StorageStreamTransaction 對象 * public IAsyncOperation<StorageStreamTransaction> OpenTransactedWriteAsync(StorageOpenOptions options) - 打開文件,返回 StorageStreamTransaction 對象 */ using System; using Windows.Storage; using Windows.Storage.Streams; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Windows10.FileSystem { public sealed partial class ReadWriteStream : Page { public ReadWriteStream() { this.InitializeComponent(); } // Write Stream(通過 IRandomAccessStream 寫) private async void btnWriteStream1_Click(object sender, RoutedEventArgs e) { // 在指定的目錄下創建指定的文件 StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary); StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdStream.txt", CreationCollisionOption.ReplaceExisting); string textContent = "I am webabcd(IRandomAccessStream)"; if (storageFile != null) { using (IRandomAccessStream randomStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite)) { using (DataWriter dataWriter = new DataWriter(randomStream)) { // 將字符串寫入數據流 dataWriter.WriteString(textContent); // 將數據流寫入文件 await dataWriter.StoreAsync(); lblMsg.Text = "寫入成功"; } } } } // Write Stream(通過 StorageStreamTransaction 寫) private async void btnWriteStream2_Click(object sender, RoutedEventArgs e) { // 在指定的目錄下創建指定的文件 StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary); StorageFile storageFile = await storageFolder.CreateFileAsync("webabcdStream.txt", CreationCollisionOption.ReplaceExisting); string textContent = "I am webabcd(StorageStreamTransaction)"; using (StorageStreamTransaction transaction = await storageFile.OpenTransactedWriteAsync()) { using (DataWriter dataWriter = new DataWriter(transaction.Stream)) { // 將字符串寫入數據流 dataWriter.WriteString(textContent); // 使用 StorageStreamTransaction 方式的話,調用 DataWriter 的 StoreAsync() 方法的作用是把數據寫入臨時文件 // 以本例為例,這個臨時文件就是同目錄下名為 webabcdStream.txt.~tmp 的文件。只要不調用 CommitAsync() 方法的話就會看到 // 返回值為寫入數據的大小,需要通過此值更新 StorageStreamTransaction 中的 Stream 的大小 transaction.Stream.Size = await dataWriter.StoreAsync(); // 重命名臨時文件 await transaction.CommitAsync(); lblMsg.Text = "寫入成功"; } } } private async void btnReadStream_Click(object sender, RoutedEventArgs e) { // 在指定的目錄下獲取指定的文件 StorageFolder storageFolder = await KnownFolders.GetFolderForUserAsync(null, KnownFolderId.DocumentsLibrary); StorageFile storageFile = await storageFolder.GetFileAsync("webabcdStream.txt"); if (storageFile != null) { using (IRandomAccessStream randomStream = await storageFile.OpenAsync(FileAccessMode.Read)) { using (DataReader dataReader = new DataReader(randomStream)) { ulong size = randomStream.Size; if (size <= uint.MaxValue) { // 從數據流中讀取數據 uint numBytesLoaded = await dataReader.LoadAsync((uint)size); // 將讀取到的數據轉換為字符串 string fileContent = dataReader.ReadString(numBytesLoaded); lblMsg.Text = "讀取結果:" + fileContent; } } } } } } }
OK
[源碼下載]