使用Stream writer将特定字节写入文本文件 [英] using Stream writer to Write a specific bytes to textfile

查看:105
本文介绍了使用Stream writer将特定字节写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我正在尝试向文本文件中写入一些值和字符串。

,但是此文本文件必须包含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:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆