StreamWriter构造函数要求文件存在,否则它将创建该文件 [英] StreamWriter constructor requires the file to exist, or else it creates it

查看:98
本文介绍了StreamWriter构造函数要求文件存在,否则它将创建该文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码:

Here is my code:

FileInfo FIFileIn = new FileInfo(@"C:\TestData.csv");
StreamReader FileIn = FIFileIn.OpenText();
string FileInStr;
string[] FileInTokens;
bool FileOutOpen = false;

// Skip past the column heading line (1st line) in the input file.
FileInStr = FileIn.ReadLine();

DateTime PrevReading = DateTime.MinValue;
string PrevReadingStr = PrevReading.ToShortDateString();

// FileInfo FIFileOut = new FileInfo(string.Empty);
// StreamWriter FileOut = FIFileOut.OpenWrite();

StreamWriter FileOut;

while (!FileIn.EndOfStream)
{
  FileInStr = FileIn.ReadLine();
  FileInTokens = FileInStr.Split(',');

  bool TokenIsDate = true;
  DateTime DTReading = new DateTime();
  string DTReadingStr = DTReading.ToString();

  try
  {
    DTReading = DateTime.Parse(FileInTokens[1]);
  }
  catch
  {
    TokenIsDate = false;
  }

  if (TokenIsDate)
  {
    DTReadingStr = DTReading.ToShortDateString();
    if (DTReadingStr != PrevReadingStr)
    {
      string CSVFilename =
        @"C:\RRTest" +
        DTReading.Year.ToString() +
        DTReading.Month.ToString() +
        DTReading.Day.ToString();

      if (FileOutOpen)
      {
        if (FileOut != null)
          FileOut.Close();
      }

      FileOut = new StreamWriter(CSVFilename);
      FileOutOpen = true;

      DTReadingStr = DTReading.ToShortDateString();
      PrevReadingStr = DTReadingStr;
    }

    FileOut.WriteLine(string.Format("{0},{1},{2},{3},{4},{5}", FileInTokens[0], FileInTokens[1], FileInTokens[2], FileInTokens[3], FileInTokens[4], FileInTokens[5]));
  }
}

FileIn.Close();
if (FileOut != null)
  FileOut.Close();



我不知道如何声明FileOut,这样在最后关闭时就不会出现编译器错误,而且我也没有所有这些难看的"if (FileOut != null)"语句.

任何建议/指针,不胜感激!

Richard



I wonder how I can declare FileOut, so that I don''t have a compiler error on the final close, and I don''t have all these ugly "if (FileOut != null)" statements.

Any advice/pointers much appreciated!

Richard

推荐答案

我建​​议重构您的代码.

1.处理输入文件并将结果放在列表中.您可能要使用LINQ GroupBy并选择第一个
2. a.使用TPL(任务并行库)使用lambda将项目写入文件.

b.循环列表并写入项目(使用lambda)

使用使用 [
I suggest to refactor your code.

1. Process the input file and put the results in a list. You might want to use LINQ GroupBy and select the first one
2. a. Use TPL (Task Parallel Library) to write the items to files using a lambda
OR
b. Loop the list and write items (use lambda)

Use the using[^] statement to handle the close for you


为什么它们丑陋" ?
它们在那里是有充分的理由-确保如果您打开了一个文件,则可以正确关闭它.
您可以将代码移入方法中,如果它会使您感到不舒服,但我会保留它的大部分内容,可能只是进行更改
Why are they "ugly"?
They are there for a good reason - to ensure that if you have opened a file, that you close it properly.
You could move the code into a method, if it upsets you that much, but I would leave it pretty much as it is, possibly just changing
if (FileOutOpen)
{
  if (FileOut != null)
    FileOut.Close();
}


if (FileOutOpen && FileOut != null)
    {
    FileOut.Close();
    }

然后,请与您的大括号保持一致!
如果需要在具有单个语句的if的语句部分中使用大括号,则应该在所包含的语句的语句上使用大括号:

And please, be consistent with your braces!
If you need braces in the statement part of one if that has a single statement, then you should have braces on the statement of the contained statement:

if (FileOutOpen)
{
  if (FileOut != null)
    FileOut.Close();
}

会更加一致,如:

Would be more consistent as:

if (FileOutOpen)
{
  if (FileOut != null)
  {
    FileOut.Close();
  }
}


混合样式不是一个好主意!


Mixing styles is not a good idea!


这篇关于StreamWriter构造函数要求文件存在,否则它将创建该文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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