[源码下载]

背水一战 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
[源码下载]

背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据的更多相关文章

  1. 背水一战 Windows 10 (92) - 文件系统: 读写“最近访问列表”和“未来访问列表”, 管理以及使用索引

    [源码下载] 背水一战 Windows 10 (92) - 文件系统: 读写“最近访问列表”和“未来访问列表”, 管理以及使用索引 作者:webabcd 介绍背水一战 Windows 10 之 文件系 ...

  2. 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Application Data 中的媒体

    [源码下载] 背水一战 Windows 10 (91) - 文件系统: Application Data 中的文件操作, Application Data 中的“设置”操作, 通过 uri 引用 Ap ...

  3. 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理

    [源码下载] 背水一战 Windows 10 (90) - 文件系统: 获取 Package 中的文件, 可移动存储中的文件操作, “库”管理 作者:webabcd 介绍背水一战 Windows 10 ...

  4. 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件

    [源码下载] 背水一战 Windows 10 (88) - 文件系统: 操作文件夹和文件 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 创建文件夹,重命名文件夹,删除文件夹, ...

  5. 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图

    [源码下载] 背水一战 Windows 10 (87) - 文件系统: 获取文件的属性, 修改文件的属性, 获取文件的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获 ...

  6. 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图

    [源码下载] 背水一战 Windows 10 (86) - 文件系统: 获取文件夹的属性, 获取文件夹的缩略图 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 获取文件夹的属性 ...

  7. 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件

    [源码下载] 背水一战 Windows 10 (85) - 文件系统: 获取文件夹和文件, 分组文件夹, 排序过滤文件夹和文件, 搜索文件 作者:webabcd 介绍背水一战 Windows 10 之 ...

  8. 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组

    [源码下载] 背水一战 Windows 10 (51) - 控件(集合类): ItemsControl - 项模板选择器, 数据分组 作者:webabcd 介绍背水一战 Windows 10 之 控件 ...

  9. 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox

    [源码下载] 背水一战 Windows 10 (30) - 控件(文本类): AutoSuggestBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(文本类) AutoSug ...

随机推荐

  1. IDEA常用快捷键,收藏以备后用

    IntelliJ Idea 常用快捷键列表 Ctrl+Shift + Enter,语句完成“!”,否定完成,输入表达式时按 “!”键Ctrl+E,最近的文件Ctrl+Shift+E,最近更改的文件Sh ...

  2. threading join用法

    join():在子线程完成运行之前,这个子线程的父线程将一直被阻塞 import threading #线程import time def Beijing(n): print('Beijing tim ...

  3. 利用nginx添加账号密码验证

    server { listen ; server_name xxx.com; location / { proxy_pass http://10.10.10.10:5601; proxy_redire ...

  4. python 的包的导入

    已经写过一篇包的导入了,最近又遇到了点问题,所以想把这些再搞的明白点就又试了试 代码结构如下 在test目录下,有Admin包,home包,在home下有它的子包foo 各个文件代码如下 admins ...

  5. linux目录与文件权限的意义

    现在我们已经知道了Linux系统内文件的三种身份(所有者,用户者,与其他人),知道每种身份都有三种属性(r,w,x),已经能够使用chown,chgrp,chmod去修改这些权限和属性,那么这些文件权 ...

  6. XSLT 创建CDATA节点

    创建文本结点 (1)直接写入文本: text1 (2)通过<xsl:text>创建文本结点: <xsl:text>text2</xsl:text> (3)通过< ...

  7. 安装好ubuntu双系统启动时卡死解决办法

    问题描述:在安装完ubuntu双系统后,第一次启动ubuntu系统时,卡死在启动界面(或者黑屏),这大概都是由于显卡驱动的原因,具体不在这里阐述,通过以下方法能成功解决,据我个人经验,这可能是诸多方法 ...

  8. 动态创建js脚本和 css样式

    //1.动态添加外部js文件 function loadScript(url){ var script = document.createElement("script"); sc ...

  9. 【笔记】计算机原理,网络,Linux操作系统

    一,计算机原理 1,化繁为简的思想,产生二进制,产生把所有计算归结为加法运算 二,网络 1,分层思想,产生ISO七层协议,商用了TCP/IP的五层协议 三,Linux操作系统 1,分层思想,硬件-&g ...

  10. Kivy 从memory 读取image

    借助PIL来处理的图片数据 fp = BytesIO() img = Image.frombytes('RGB', img_size, buf_bytes, 'raw', 'BGR;16', 0, 1 ...