Streamwriter有时会在一行的中间截断我的最后两行? [英] Streamwriter is cutting off my last couple of lines sometimes in the middle of a line?

查看:41
本文介绍了Streamwriter有时会在一行的中间截断我的最后两行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码.:

FileStream fileStreamRead = new FileStream(pathAndFileName, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
FileStream fileStreamWrite = new FileStream(reProcessedFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);

                StreamWriter sw = new StreamWriter(fileStreamWrite);

                int readIndex = 0;
                using (StreamReader sr = new StreamReader(fileStreamRead))
                {
                    while (!sr.EndOfStream) {
                        Console.WriteLine("eof" + sr.EndOfStream);
                        readIndex++;
                        Console.WriteLine(readIndex);
                        string currentRecord = "";
                        currentRecord = sr.ReadLine();
                        if (currentRecord.Trim() != "")
                        {
                            Console.WriteLine("Writing " + readIndex);
                            sw.WriteLine(currentRecord);
                        }
                        else {
                            Console.WriteLine("*******************************************spaces ***********************");
                        }
                    }

它正在用一个测试文件剪掉2行,并剪掉一半行,然后将我要对其进行测试的另一测试文件剪掉1行和半行.

It is cutting off 2 lines with one test file and half a line, and then 1 line and half a line with the other test file I am running it against.

我不是您可能会看到的流阅读器/编写器专家.

I am not a streamreader/writer expert you can probably see.

任何想法或建议都将不胜感激,因为这正推动着我前进.我确定是我不正确地使用了它们.

Any ideas or suggestions would be greatly appreciated as this is driving me batty. I am sure it is me using these incorrectly.

推荐答案

您缺少编写者的刷新/关闭"功能,或者只是使用.

You are missing Flush/Close or simply using for your writer.

using(FileStream fileStreamWrite = 
  new FileStream(reProcessedFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
{
  using(StreamWriter sw = new StreamWriter(fileStreamWrite))
  {
   // .... write everything here
  }
}

这篇关于Streamwriter有时会在一行的中间截断我的最后两行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆