Streamwriter仅从字符串列表中写入一定数量的字节 [英] Streamwriter only writes a certain amount of bytes from a string list

查看:64
本文介绍了Streamwriter仅从字符串列表中写入一定数量的字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 27 csv files containing employee timesheet data with about 2000 lines of data in total, and is growing day by day.

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

I am writing an application that compiles the data in each file to a single csv file for analysis in Excel. I do this by reading each file, write its contents to a string list and then write the string list to a new file. Below is the code writing the list to the file:

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();

reader 是一个包含约1170行时间表条目的列表.当我查询 reader 的内容时,所有数据都会显示.

reader is a list containing about 1170 lines of timesheet entries. When I query the contentents of reader all of the data is present.

当我打开已编译的csv时,问题就来了,除了最后一个时间表文件的最后几行,所有数据都存在.编译后的文件仅包含1167条全行,其中1168行在任意点处被截断.在检查了具有不同内容的不同文件之后,我注意到每个文件的大小正好为160840bytes.

The problem comes when I open the compiled csv, all of the data is present except for the last few lines of the last timesheet file. The compiled file contains only 1167 full lines with line 1168 cut off at some arbitrary point. After scrutinizing different files with different contents I noticed that each file is exactly 160840bytes in size.

出于调试目的,我修改了如下代码:

For debugging purposes I modified the code like this:

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);

进行此修改后,运行代码后弹出的消息将显示应该已写入文件的正确条目(但不是);这是 foreach 循环遍历所有数据的证据.现在,已编译文件的大小较大,约为200kB.当我仔细检查时,所有的"testline" 行都被添加了,实际的时间表条目仍在与"testline" 行所处的相同任意点处被切断没有写.

With this modification the message that pops up after the code is run displays the correct entry that should have been written to the file (but is not); this is evidence that the foreach loop loops through all the data. The compiled file now has a larger size of around 200kB. When I scrutinize it, all of the "testline" lines are added, the actual timesheet entry is still cut off at the same arbitrary point as when the "testline" lines were not written.

总结:

  • 我正在尝试将字符串列表写入文件
  • 所有必需的数据都在列表中
  • 代码循环遍历列表中的所有数据
  • 最终的书面文件未包含列表中的所有数据

有人可以告诉我发生了什么问题吗?我将尝试将内容写入文件的另一种方法,但是我仍然想知道这里会发生什么以及如何防止它发生.

Can anyone advise me on what is going wrong? I will try a different approach to write the contents to the file, but I would still like to know what happens here and what I can do to prevent it.

推荐答案

StreamWriter 类具有自己的缓冲区,如果不处理/关闭它,则缓冲区的其余部分将丢失,就像您观察到的一样.

The StreamWriter class has its own buffer, if you don't dispose/close it, the rest of the buffer will be lost, exactly as you've observed.

有多种刷新该缓冲区的方法.

There are multiple ways to flush this buffer.

您可以显式刷新它:

sw.Flush();

但这不是我想要提供的建议,相反,您应该处理StreamWriter实例,该实例还会将缓冲区刷新到基础流,并且作为一般准则,任何实现 IDisposable 处理完毕后,应将其丢弃.

but that's not the advice I would want to give, instead you should dispose of the StreamWriter instance which will also flush the buffer to the underlying stream, and as a general guideline, any object that implements IDisposable should be disposed of when you're done with it.

因此,像这样修改您的代码:

So modify your code like this:

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);
    }
}

如果您想更深入地研究 StreamWriter 类的工作方式,建议您检查

If you want to dig deeper into how the StreamWriter class works I suggest examining the Reference Source Code of StreamWriter.

这篇关于Streamwriter仅从字符串列表中写入一定数量的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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