使用流编写器将特定字节写入文本文件

using Stream writer to Write a specific bytes to textfile(使用流编写器将特定字节写入文本文件)
本文介绍了使用流编写器将特定字节写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在尝试将一些值和字符串写入文本文件.
但是这个文本文件必须包含 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:

这篇关于使用流编写器将特定字节写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)