日期:2014-05-18  浏览次数:20868 次

BinaryWriter类,怎么没写文件?
BinaryWriter类:MSDN,以二进制形式将基元类型写入流,并支持用特定的编码写入字符串。

 static void Main(string[] args)
  {
  bool b = true;
  BinaryWriter bw = new BinaryWriter(new FileStream(@"d:\love.txt", FileMode.Open), Encoding.UTF8);
  bw.Write(b);  
  Console.ReadKey();
  }


上面代码使用BinaryWriter类的 Write 方法编写布尔值到流为一个字节值,为什么打开文件,里面什么都没写呢?

------解决方案--------------------
bool b = true;
BinaryWriter bw = new BinaryWriter(new FileStream(@"d:\love.txt", FileMode.OpenOrCreate), Encoding.UTF8);
bw.Write(b);
bw.Flush();
bw.Close();
------解决方案--------------------
bw.Close();关闭后,才会写入文件
------解决方案--------------------
探讨

bw.Close();关闭后,才会写入文件

------解决方案--------------------
因为并没有写到文件里,而是写到了内存里,得close才行
------解决方案--------------------
你有没有按键盘让程序运行完?

或者去掉Console.ReadKey();

------解决方案--------------------
探讨

引用:
你有没有按键盘让程序运行完?

或者去掉Console.ReadKey();

去掉了Console.ReadKey();
还是没有写进东西啊

------解决方案--------------------
探讨

引用:

引用:
你有没有按键盘让程序运行完?

或者去掉Console.ReadKey();

去掉了Console.ReadKey();
还是没有写进东西啊
new FileStream(@"d:\love.txt", FileMode.Open) 这个流关了 蛋疼啊

------解决方案--------------------
System.IO.FileStream fs = new System.IO.FileStream(@"d:\love.txt", System.IO.FileMode.Open);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(fs);
bw.Write(b);
bw.Close();
fs.Close();
bw = null;
fs = null;