4

我对 C# 中的文件有点陌生并且遇到了问题。从一个文件读取并复制到另一个文件时,最后一块文本没有被写入。下面是我的代码:

StringBuilder sb = new StringBuilder(8192);
string fileName = "C:...rest of path...inputFile.txt";
string outputFile = "C:...rest of path...outputFile.txt";

using (StreamReader reader = File.OpenText(fileName))
{
   char[] buffer = new char[8192];
   while ((reader.ReadBlock(buffer, 0, buffer.Length)) != 0)
   {
      foreach (char c in buffer)
      {
         //do some function on char c... 
         sb.Append(c);
      }

      using (StreamWriter writer = File.CreateText(outputFile))
      {
         writer.Write(sb.ToString());
      }
   }
}

我的目标是以缓冲的方式读取和写入文本文件。在 Java 中,我可以通过以下方式实现:

public void encrypt(File inputFile, File outputFile) throws IOException
{
   BufferedReader infromfile = null;
   BufferedWriter outtofile = null;

   try
   {
      String key = getKeyfromFile(keyFile);
      if (key != null)
      {
         infromfile = new BufferedReader(new FileReader(inputFile));
         outtofile = new BufferedWriter(new FileWriter(outputFile));
         char[] buffer = new char[8192];
         while ((infromfile.read(buffer, 0, buffer.length)) != -1)
         {
            String temptext = String.valueOf(buffer);
            //some changes to temptext are done
            outtofile.write(temptext);
         }
      }
   }
   catch (FileNotFoundException exc)
   {
   } // and all other possible exceptions
}

你能帮我找出问题的根源吗?

如果您认为可能有更好的方法来使用文本文件实现缓冲 i/o,我将非常感谢您的建议。

4

5 回答 5

1

有几个“陷阱”:

  1. c无法更改(它是 foreach 迭代变量),您需要复制它以便在编写之前进行处理
  2. 您必须跟踪缓冲区的大小,ReadBlock用会使输出变脏的字符填充它

像这样更改代码看起来很有效:

//extracted from your code
foreach (char c in buffer)
{
    if (c == (char)0) break; //GOTCHA #2: maybe you don't want NULL (ascii 0) characters in your output

    char d = c; //GOTCHA #1: you can't change 'c'

    // d = SomeProcessingHere();

    sb.Append(d);
}
于 2012-05-17T14:18:12.570 回答
1

尝试这个:

        string fileName = @"";
        string outputfile = @"";

        StreamReader reader = File.OpenText(fileName);
        string texto = reader.ReadToEnd();

        StreamWriter writer = new StreamWriter(outputfile);
        writer.Write(texto);

        writer.Flush();
        writer.Close();
于 2012-05-17T14:23:46.803 回答
0

除非我遗漏了什么,否则您的问题似乎是您在每次块读取迭代时都覆盖了输出文件的现有内容。

你打电话:

  using (StreamWriter writer = File.CreateText(outputFile))
  {
     writer.Write(sb.ToString());
  }

对于每个 ReadBlock 迭代。文件的输出只是被读取的最后一块数据。

来自File.CreateText 上的MSDN文档:

如果 path 指定的文件不存在,则创建该文件。如果文件确实存在,则其内容将被覆盖。

于 2012-05-17T14:39:24.520 回答
0

如果你不关心carraign return,你可以使用File.ReadAllText

此方法打开一个文件,读取文件的每一行,然后将每一行添加为字符串的一个元素。然后它关闭文件。行定义为字符序列后跟回车符 ('\r')、换行符 ('\n') 或回车符后紧跟换行符。结果字符串不包含终止回车和/或换行。

StringBuilder sb = new StringBuilder(8192);
string fileName = "C:...rest of path...inputFile.txt";
string outputFile = "C:...rest of path...outputFile.txt";

// Open the file to read from.
string readText = File.ReadAllText(fileName );
foreach (char c in readText)
{
   // do something to c
   sb.Append(new_c);
}

// This text is added only once to the file, overwrite it if it exists
File.WriteAllText(outputFile, sb.ToString());        
于 2012-05-17T14:20:35.373 回答
0

这对你有用吗?

       using (StreamReader reader = File.OpenText(fileName))
       {
            char[] buffer = new char[8192];
            bool eof = false;

            while (!eof)
            {
                int numBytes = (reader.ReadBlock(buffer, 0, buffer.Length));
                if (numBytes>0)
                {
                    using (StreamWriter writer = File.CreateText(outputFile))
                    {
                        writer.Write(buffer, 0, numBytes);
                    }
                } else {
                    eof = true;
                }

            }
        }

不过,您仍然必须注意字符编码!

于 2012-05-17T14:23:34.513 回答