用于写入文件字符串和byte []数组的流类是什么? 如果文件不存在,则需要打开文件以追加或创建新文件。
using (Stream s = new Stream("application.log")
{
s.Write("message")
s.Write(new byte[] { 1, 2, 3, 4, 5 });
}
答案 0 :(得分:6)
尝试使用BinaryWriter? http://msdn.microsoft.com/en-us/library/system.io.binarywriter.aspx
答案 1 :(得分:6)
使用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更好用。
答案 2 :(得分:1)
也许你需要一些更简单的东西?
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");
答案 3 :(得分:0)
尝试BinaryWriter。