using Stream writer to Write a specific bytes to t

2019-04-09 06:09发布

问题:

Well I'm trying to write some values and strings to a text file.
but this text file must contain 2 bytes

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.

回答1:

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
                 });
}

When opening the "Bytes.data" file with a hex editor you should see these bytes:



回答2:

I just figured this out. It works quite well for me. The idea is you open the file with a FileStream that can write byte arrays, and put a StreamWriter on top of it to write strings. And then you can use both to mix strings with your bytes:

// StreamWriter writer = new StreamWriter(new FileStream("file.txt", FileMode.OpenOrCreate));

byte[] bytes = new byte[] { 0xff, 0xfe };
writer.BaseStream.Write(bytes, 0, bytes.Length);


回答3:

If I understand correctly, you're trying to write some strings to a text file, but you want to add 2 bytes to this file.

Why won't you try using: File.WriteAllBytes ?

Convert your string to a Byte array using

byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(str); // If your using UTF8

Create a new byte array from the original byteArray with the additional 2 bytes.

And write them to a file using:

File.WriteAllBytes("MyFile.dat", newByteArray)


回答4:

Here is one more way to look for a solution...

StringBuilder sb = new StringBuilder();

sb.Append("Hello!! ").Append(",");
sb.Append("My").Append(",");
sb.Append("name").Append(",");
sb.Append("is").Append(",");
sb.Append("Rajesh");
sb.AppendLine();

//use UTF8Encoding(true) if you want to use Byte Order Mark (BOM)
UTF8Encoding utf8withNoBOM = new UTF8Encoding(false);

byte[] bytearray;

bytearray = utf8withNoBOM.GetBytes(sb.ToString());

using (FileStream fileStream = new FileStream(System.Web.HttpContext.Current.Request.MapPath("~/" + "MyFileName.csv"), FileMode.Append, FileAccess.Write))
{
    StreamWriter sw = new StreamWriter(fileStream, utf8withNoBOM);

    //StreamWriter for writing bytestream array to file document
    sw.BaseStream.Write(bytearray, 0, bytearray.Length);
    sw.Flush();
    sw.Close();

    fileStream.Close();
}


回答5:

There is a StreamWriter.Write(char) that will write a 16-bit value. You should be able to set your variable with the hex value like char val = '\xFFFE' and pass it to Write. You could also use FileStream where all the Write methods work off bytes, and it specifically has a WriteByte(byte) method. The MSDN documentation for it gives an example of outputting UTF8 text.



回答6:

After saving the string simply write those bytes using for example using File.WriteAllBytes or a BinaryWriter: Can a Byte[] Array be written to a file in C#?