Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Streamwriter仅从字符串列表中写入一定数量的字节_C#_List_File Read - Fatal编程技术网

C# Streamwriter仅从字符串列表中写入一定数量的字节

C# Streamwriter仅从字符串列表中写入一定数量的字节,c#,list,file-read,C#,List,File Read,我有27个csv文件,其中包含员工时间表数据,总共有大约2000行数据,并且每天都在增长 我正在编写一个应用程序,将每个文件中的数据编译成单个csv文件,以便在Excel中进行分析。我通过读取每个文件,将其内容写入字符串列表,然后将字符串列表写入新文件来实现这一点。下面是将列表写入文件的代码: FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite); St

我有27个csv文件,其中包含员工时间表数据,总共有大约2000行数据,并且每天都在增长

我正在编写一个应用程序,将每个文件中的数据编译成单个csv文件,以便在Excel中进行分析。我通过读取每个文件,将其内容写入字符串列表,然后将字符串列表写入新文件来实现这一点。下面是将列表写入文件的代码:

FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
foreach (string l in reader)//reader is a List<string> pouplated earlier
{
    sw.WriteLine(l);
}
fs.Close();
通过此修改,代码运行后弹出的消息显示应写入文件(但未写入)的正确条目;这是foreach循环遍历所有数据的证据。编译后的文件现在有大约200kB的更大大小。当我仔细检查时,所有的
“testline”
行都被添加了,实际的时间表条目仍然在
“testline”
行未写入时的同一任意点被截断

总之:

  • 我正在尝试将字符串列表写入文件
  • 列表中提供了所有必要的数据
  • 代码循环遍历列表中的所有数据
  • 最终写入的文件不包含列表中的所有数据

有人能告诉我出了什么问题吗?我将尝试一种不同的方法将内容写入文件,但我仍然想知道这里发生了什么,以及我可以做些什么来防止它。

StreamWriter类有自己的缓冲区,如果您不处置/关闭它,缓冲区的其余部分将丢失,正如您所观察到的那样

有多种方法可以刷新此缓冲区

您可以显式刷新它:

sw.Flush();
但这不是我想要给出的建议,相反,您应该处理StreamWriter实例,它也会将缓冲区刷新到底层流,并且作为一般指导原则,任何实现
IDisposable
的对象都应该在处理完后处理掉

因此,请按如下方式修改代码:

string line = "";//A string to test whether the foreach loop completes
FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
foreach (string l in reader)//reader is a List<string> pouplated earlier
{
    line = l;
    sw.WriteLine(line);
    sw.WriteLine("testline")//write random content to see if it has effect on the data
}
fs.Close();
MessageBox.Show(line);
using (FileStream fs = new FileStream(saveFileDialog1.FileName, FileMode.CreateNew, FileAccess.ReadWrite))
using (StreamWriter sw = new StreamWriter(fs))
{
    foreach (string l in reader)//reader is a List<string> pouplated earlier
    {
        sw.WriteLine(l);
    }
}
使用(FileStream fs=newfilestream(saveFileDialog1.FileName、FileMode.CreateNew、FileAccess.ReadWrite))
使用(StreamWriter sw=新StreamWriter(fs))
{
foreach(reader中的字符串l)//reader是一个列表
{
西南写入线(l);
}
}

如果您想深入了解
StreamWriter
类的工作原理,我建议检查。

在结束之前调用fs.Flush()是否也能达到预期的结果?是的,有多种方法可以到达终点,但我坚持我的指导原则,始终处理所有
IDisposable
对象。这很公平。似乎是一个合理的指导方针。不过编辑了我的答案,以至少显示同花顺,并解释为什么处理有效。我认为你应该删除同花顺的建议。使用的解决方案在各方面都更好。