现在有一个1.txt文件,内容是一个1,用二进制的方式把他读到另外一个文件2.txt中是49,不知道为什么是49(这个不重要),然后读2.txt文件,要读到的内容是1,怎么样读呢。读到的变量data是49,求救啊!!!!!!!!!!!
string pathbody = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\1.txt";
string pathheader = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\2.txt";
            byte[] arrFile=null;
            using (FileStream fs2 = new FileStream(pathbody, FileMode.Open)) 
            {
                arrFile= new byte[1];
                fs2.Read(arrFile, 0, 1);//读1.txt的文件
                using (StreamReader sr = new StreamReader(fs2))
                {
                   using (StreamWriter sw1 = new StreamWriter(pathheader, true))
                   {
                       for (int i = 0; i < 1; i++)
                       {
                           sw1.Write(arrFile[i]);                       }
                   }
                }            }            using (FileStream fsBody = new FileStream(pathheader, FileMode.Open, FileAccess.Read))
            {                using (StreamReader sw1 = new StreamReader(fsBody))
                {
                    string str = sw1.ReadLine();
                    byte[] demo = System.Text.Encoding.Default.GetBytes(str);
                    string data = System.Text.Encoding.Default.GetString(demo);
                    
                }
            }

解决方案 »

  1.   

    http://www.cnblogs.com/qiantuwuliang/archive/2009/03/12/1409417.html
      

  2.   

    LZ以下代码是读取.txt值的例子
    ------------------------   if (File.Exists("路径/XX.txt"))
                {
                    if (!this.txtAA.AutoCompleteCustomSource.Contains(txtAA.Text))//判断记录是否存在
                    {
                        StreamWriter sw = new StreamWriter("路径/XX.txt", false);//true参数不可少,否则会覆盖以前存入的记录
                        sw.WriteLine(this.txtAA.Text.Trim());//存入记录
                        sw.Close();
                        if (!this.txtAA.AutoCompleteCustomSource.Contains(this.txtAA.Text))
                        {
                            this.txtAA.AutoCompleteCustomSource.Add(this.txtAA.Text);
                        }
                    }
                    this.DialogResult = DialogResult.OK;
                } 
      

  3.   

    你读到49是正确的,49就是字符“1”,字符“1”的ascii码和UTF-8,UTF7编码都是49,你把49写入另一个txt文件,然后打开看,还是1,因为49就是字符“1”,
      

  4.   

    这才是读取
    ---------   if (File.Exists(@"../../XX.txt"))//路径和文件类型大家自己设置
                {
                    StreamReader sr = new StreamReader(@"../../XX.txt", true);                string str = sr.ReadLine();
                    if (str != null)
                    {
                        txtAA.Text = str.Trim();
                    }
                    sr.Close();
                }
      

  5.   

    为了避免这类问题,应该统一用StreamReader和StreamWriter来读写,不要用FileStream直接读写,
      

  6.   

    这样之后读出来的2.txt就是1了,string pathbody = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\1.txt";
    string pathheader = @"C:\Documents and Settings\Administrator\桌面\新建文件夹 (2)" + "\\2.txt";
      byte[] arrFile=null;
      using (FileStream fs2 = new FileStream(pathbody, FileMode.Open))  
      {
      arrFile= new byte[1];
      fs2.Read(arrFile, 0, 1);//读1.txt的文件
      string sData = System.Text.Encoding.Default.GetString(arrFile);
      using (StreamReader sr = new StreamReader(fs2))
      {
      using (StreamWriter sw1 = new StreamWriter(pathheader, true))
      {
      for (int i = 0; i < 1; i++)
      {
      sw1.Write(sData );  }
      }
      }  }  using (FileStream fsBody = new FileStream(pathheader, FileMode.Open, FileAccess.Read))
      {  using (StreamReader sw1 = new StreamReader(fsBody))
      {
      string str = sw1.ReadLine();
      byte[] demo = System.Text.Encoding.Default.GetBytes(str);
      string data = System.Text.Encoding.Default.GetString(demo);
        
      }
      }
      

  7.   


    我是想把1.txt内容存到2.txt文件的是二进制.你这样写存的是一样的内容。
      

  8.   

    应该是二进制存储就是49,不要用byte,直接readline就好了
      

  9.   

    <a href="http://blog.csdn.net/nidexuanzhe/article/details/6060869">
    看看去
    </a>
      

  10.   


    4楼正解,1的ASCII正是49,转换一下就行了,他已经在下面给出解决办法了!