看,我正在编写一个简单的程序来替换CSV文件中的数据。
我需要替换的是第2列(父列(来设置ID的名称,而不是ID,如下所示:
原始文件:
ID - PARENT - NAME
ID 1 - - Name of ID 1
ID 2 - ID 1 - Name of ID 2
ID 3 - ID 2 - Name of ID 3
ID 4 - ID 1 - Name of ID 4
ID 5 - ID 4 - Name of ID 5
结果:
ID - PARENT - NAME
ID 1 - - Name of ID 1
ID 2 - Name of ID 1 - Name of ID 2
ID 3 - Name of ID 2 - Name of ID 3
ID 4 - Name of ID 1 - Name of ID 4
ID 5 - Name of ID 4 - Name of ID 5
因此,我需要在这里使用同一StreamReader的两个实例进行递归循环,以比较每个实例。我不是问如何进行循环,而是问如何同时使用两个流读取器,因为我试图通过创建所有必要的变量来实现,但C#似乎不接受这一点,而且它根本不起作用,数据都是空白的。。。并且也没有给出任何错误。我该怎么做?
起作用的部分:
filePath = openFileDialog.FileName;
var fileStream = openFileDialog.OpenFile();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream, Encoding.GetEncoding("iso-8859-1")))
{
fileContent = reader.ReadLine();
int i = 1;
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(nomArxiu, false, Encoding.GetEncoding("iso-8859-1")))
{
while (fileContent != null)
{
/// code to replace stuff, works for one single row at a time, but not for multiple rows at the same time
}
}
writer.Flush();
writer.Close();
}
我尝试过的不起作用:
System.IO.StreamReader reader2 = reader; // create a copy of the reader object...
我想访问同一文件的两个独立流读取器,但它不能这样工作。。。
代码的问题是C#中的类是通过引用传递的,而不是通过值传递的。
这意味着这条线:
System.IO.StreamReader reader2 = reader;
是而不是按照预期创建新的StreamReader
,而是创建了对具有相同流位置的同一StreamReader
的另一个引用。
您需要做的是用一个新的FileStream
创建一个新StreamReader
,如下所示:
filePath = openFileDialog.FileName;
var fileStream = openFileDialog.OpenFile();
var secondFileStream = openFileDialog.OpenFile();
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream, Encoding.GetEncoding("iso-8859-1")))
using (System.IO.StreamReader reader2 = new System.IO.StreamReader(secondFileStream, Encoding.GetEncoding("iso-8859-1")))
{
fileContent = reader.ReadLine();
int i = 1;
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(nomArxiu, false, Encoding.GetEncoding("iso-8859-1")))
{
while (fileContent != null)
{
/// code to replace stuff, works for one single row at a time, but not for multiple rows at the same time
}
}
writer.Flush();
writer.Close();
}
我不确定运行OpenFileDialog.OpenFile()
两次的功能,所以如果不起作用,请尝试以下操作:
filePath = openFileDialog.FileName;
using (FileStream fileStream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (FileStream secondFileStream = File.Open(openFileDialog.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
using (System.IO.StreamReader reader = new System.IO.StreamReader(fileStream, Encoding.GetEncoding("iso-8859-1")))
using (System.IO.StreamReader reader2 = new System.IO.StreamReader(secondFileStream, Encoding.GetEncoding("iso-8859-1")))
{
fileContent = reader.ReadLine();
int i = 1;
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(nomArxiu, false, Encoding.GetEncoding("iso-8859-1")))
{
while (fileContent != null)
{
/// code to replace stuff, works for one single row at a time, but not for multiple rows at the same time
}
}
writer.Flush();
writer.Close();
}