流写入字符串和字节[]数组



用于写入文件字符串和byte[]数组的流类是什么?如果文件不存在,则需要打开文件追加或创建新文件。

using (Stream s = new Stream("application.log")
{
    s.Write("message")
    s.Write(new byte[] { 1, 2, 3, 4, 5 });
}

使用BinaryWriter-Class

using (Stream s = new Stream("application.log")
{
   using(var b = new BinaryWriter(s))
   {
    b.Write(new byte[] { 1, 2, 3, 4, 5 });
   }
}

或者像Tim Schmelter建议的那样(谢谢)只要FileStream:

using (var s = new FileStream("application.log", FileMode.Append, FileAccess.Write)
{
    var bytes = new byte[] { 1, 2, 3, 4, 5 };
    s.Write(bytes, 0, bytes.Length);
}

如果需要,它将追加或创建文件,但BinaryWriter使用起来更好。

尝试使用BinaryWriter?http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx

也许你需要更简单的东西?

File.WriteAllBytes("application.log", new byte[] { 1, 2, 3 });
File.WriteAllLines("application.log", new string[] { "1", "2", "3" });
File.WriteAllText("application.log", "here is some context");

试试BinaryWriter

相关内容

  • 没有找到相关文章

最新更新