使用流编写器将特定字节写入文本文件
- 作者: 大姐有钱就他娘的甩了你
- 来源: 51数据库
- 2023-02-13
问题描述
好吧,我正在尝试将一些值和字符串写入文本文件.
但是这个文本文件必须包含 2 个字节
Well I'm trying to write some values and strings to a text file.
but this text file must contain 2 bytes
在完成向文本文件写入其他值后,这些是我想插入到文本文件中的 2 个字节:
These are the 2 bytes I want to insert to my text file after finishing writing the other values to it:
我试过这个方法,但我不知道如何通过它写字节
I tried this method but I have no idea how to write bytes through it
using (StreamWriter sw = new StreamWriter(outputFilePath, false, Encoding.UTF8))
我不知道在把我想要的字符串放在上面后如何将它们写入文本文件.
I have no idea about how to write them to the text file after putting the strings I want on it.
推荐答案
如果我没记错的话.您想将字符串写入文件,然后向其中写入字节吗?
If I recall correctly from your question. You want to write strings to a file and then write bytes to it?
这个例子会为你做到这一点:
This example will do that for you:
using (FileStream fsStream = new FileStream("Bytes.data", FileMode.Create)) using (BinaryWriter writer = new BinaryWriter(fsStream, Encoding.UTF8)) { // Writing the strings. writer.Write("The"); writer.Write(" strings"); writer.Write(" I"); writer.Write(" want"); writer.Write("."); // Writing your bytes afterwards. writer.Write(new byte[] { 0xff, 0xfe }); }
使用十六进制编辑器打开Bytes.data"文件时,您应该看到以下字节:
When opening the "Bytes.data" file with a hex editor you should see these bytes:
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源









