【问题标题】:copy a text file复制一个文本文件
【发布时间】:2010-04-29 19:35:31
【问题描述】:

我正在尝试将文本文件逐行复制到另一个文本文件中。似乎有一个 1024 字符的缓冲区。如果我的文件中少于 1024 个字符,我的函数将不会复制到另一个文件中。

另外,如果超过 1024 个字符但小于 1024 的因数,这些超出的字符将不会被复制。

例如:

初始文件中的 2048 个字符 - 复制了 2048 个

初始文件中有 988 个字符 - 复制了 0 个

初始文件中的 1256 个字符 - 已复制 1024 个

private void button3_Click(object sender, EventArgs e)
{
    // écrire code pour reprendre le nom  du fichier sélectionné et 
    //ajouter un suffix "_poly.txt"
    string ma_ligne;
    const int RMV_CARCT = 9;

    //délcaration des fichier
    FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read);
    textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt");
    FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate,FileAccess.ReadWrite);

    //lecture/ecriture des fichiers en question
  StreamReader apt = new StreamReader(apt_file);
  StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16);



  while (apt.Peek() >= 0)
  {
      ma_ligne = apt.ReadLine();
      //if (ma_ligne.StartsWith("GOTO"))
      //{
      //   ma_ligne = ma_ligne.Remove(0, RMV_CARCT);
      //   ma_ligne = ma_ligne.Replace(" ","");
      //   ma_ligne = ma_ligne.Replace(",", " ");
      mdi_line.WriteLine(ma_ligne);
      //}
  }
  apt_file.Close();
  mdi_file.Close();
}

【问题讨论】:

  • button3_Click( ... ) ... 哎呀。 ;)

标签: c# .net file text


【解决方案1】:

两个问题:

  1. 您的FileStreamStreamWriterStreamReader 类应该在using { } 块内。它们实现了IDisposable,因此您需要调用Dispose,而using 块将为您执行此操作。如果你这样做,那实际上就是你必须解决的所有问题(我将在一分钟内解释)。这样做也意味着您不再需要致电Close()
  2. 至少在关闭它之前致电mdi_line.Flush()。这将导致缓冲区立即写入文件。

StreamWriter 类上调用Dispose 会自动调用Flush,这就是using 块将纠正问题的原因。

using (FileStream apt_file = new FileStream(textBox1.Text, FileMode.Open, FileAccess.Read))
{
    textBox1.Text = textBox1.Text.Replace(".txt", "_mod.txt");

    using (FileStream mdi_file = new FileStream(textBox1.Text, FileMode.OpenOrCreate, FileAccess.ReadWrite))
    {
        //lecture/ecriture des fichiers en question 
        using (StreamReader apt = new StreamReader(apt_file))
        using (StreamWriter mdi_line = new StreamWriter(mdi_file, System.Text.Encoding.UTF8, 16))
        {
            while (apt.Peek() >= 0)
            {
                ma_ligne = apt.ReadLine();
                //if (ma_ligne.StartsWith("GOTO")) 
                //{ 
                //   ma_ligne = ma_ligne.Remove(0, RMV_CARCT); 
                //   ma_ligne = ma_ligne.Replace(" ",""); 
                //   ma_ligne = ma_ligne.Replace(",", " "); 
                mdi_line.WriteLine(ma_ligne);
                //} 
            }
        }
    }
}

【讨论】:

  • @melt:它会起作用,但你真的应该像我描述的那样使用using 块。您应该能够将该代码复制并粘贴到您的应用程序中,以替代现有的代码。
【解决方案2】:

你知道,有一个File.Copy()-方法吗?还有FileInfo has a Copy()-method。
编辑:我看到你想在复制过程中修改文件内容,虽然现在已经注释掉了。

有关 StreamReader 的用法,您可以查看msdn documentation。它建议一个没有Peek() 的实现:

 using (StreamReader sr = new StreamReader("TestFile.txt")) 
 {
      string line;
      // Read and display lines from the file until the end of 
      // the file is reached.
      while ((line = sr.ReadLine()) != null) 
      {
          Console.WriteLine(line);
      }
 }

【讨论】:

  • 看起来他在复制每一行时都在做一些处理。
  • 从注释代码看来,目标是逐行读取文件,并在写入输出文件之前修改每一行。
  • @Shane, @Martin:感谢您的指出——您可能是对的,我的回答对他的情况没有帮助
  • Thrue 但我们正在寻找特定的行并对其进行修改。如 cmets 中的代码所示。一切正常,问题是它不会复制最后几行,因为缓冲区不完整。
【解决方案3】:

你会想要的:

using (StreamReader apt= new StreamReader(apt_file)) 
{
    while (apt.Peek() > -1){ ...}
 }

见:http://msdn.microsoft.com/en-us/library/system.io.streamreader.peek.aspx

【讨论】:

  • 这与他的问题无关。
  • 我相信他所拥有的 >= 0 在这种情况下相当于 > -1,因为返回值是 int。
【解决方案4】:

您可以像这样使用ReadLine() method 读取文件:

using (StreamReader sr = new StreamReader(inputFile)) 
{
    String line;
    while ((line = sr.ReadLine()) != null) 
    {
       // process line
    }
}

【讨论】:

  • 这与他的问题无关。
【解决方案5】:

你自己缓冲,然后将 apt.ReadBlock() 放入缓冲中怎么样。然后在缓冲内容上拆分 \r\n ,处理拆分数组中的行。不要处理拆分缓冲区中的最后一行,将其添加到下一个 ReadBlock,冲洗重复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-02-26
    • 2014-12-05
    • 1970-01-01
    • 2014-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    相关资源
    最近更新 更多