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 方法编写布尔值到流为一个字节值,为什么打开文件,里面什么都没写呢?

解决方案 »

  1.   

    bool b = true;
                BinaryWriter bw = new BinaryWriter(new FileStream(@"d:\love.txt", FileMode.OpenOrCreate), Encoding.UTF8);
                bw.Write(b);
                bw.Flush();
                bw.Close();
      

  2.   

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

  3.   

    ++  
    这种问题来CSDN问人挺麻烦的(浪费时间)  自己慢慢调试一下就行了(调试和写代码同样重要)
      

  4.   

    因为并没有写到文件里,而是写到了内存里,得close才行
      

  5.   

    老大,以及楼上各位老大,试过没有啊,我加了bw.Close();还是没有写进东西啊...
      

  6.   

    你有没有按键盘让程序运行完?或者去掉Console.ReadKey();
      

  7.   

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

  8.   

    new FileStream(@"d:\love.txt", FileMode.Open)  这个流关了  蛋疼啊
      

  9.   

    得这样写FileStream ss=new FileStream(@"d:\love.txt", FileMode.Open);
     bool b = true;
      BinaryWriter bw = new BinaryWriter(ss, Encoding.UTF8);
      bw.Write(b这个蛋疼);  
      bw.Close();
      ss.Colse(); 
      Console.ReadKey();
      

  10.   

    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;