C# 如何使用StreamReader向词典添加拆分行?
我正试图用StreamReader读取一个.txt文件,拆分行并将它们添加到字典中,但当我调试它时,它不起作用,因为第一行为null,并且它不会进一步。如何定义字符串完整行以使其工作C# 如何使用StreamReader向词典添加拆分行?,c#,split,streamreader,C#,Split,Streamreader,我正试图用StreamReader读取一个.txt文件,拆分行并将它们添加到字典中,但当我调试它时,它不起作用,因为第一行为null,并且它不会进一步。如何定义字符串完整行以使其工作 StreamReader sr = new StreamReader(@"N:\Desktop\krew.txt"); StreamWriter sw = new StreamWriter(@"N:\Desktop\newKrew.txt"); Dictionary&
StreamReader sr = new StreamReader(@"N:\Desktop\krew.txt");
StreamWriter sw = new StreamWriter(@"N:\Desktop\newKrew.txt");
Dictionary<string, string> dict = new Dictionary<string, string>();
string fullLine = "";
while (fullLine != null)
{
fullLine = sr.ReadLine();
string[] wholeLine = fullLine.Split('\t');
dict.Add(wholeLine[0], wholeLine[1]);
StreamReader sr=newstreamreader(@“N:\Desktop\krew.txt”);
StreamWriter sw=新StreamWriter(@“N:\Desktop\newKrew.txt”);
Dictionary dict=新字典();
字符串fullLine=“”;
while(fullLine!=null)
{
fullLine=sr.ReadLine();
string[]wholeLine=fullLine.Split('\t');
dict.Add(整行[0],整行[1]);
您可以尝试Linq并让.Net为您打开(和Dispose
)Stream
s、Reader
s
using System.Linq;
...
Dictionary<string, string> dict = File
.ReadLines(@"N:\Desktop\krew.txt")
.Where(line => !string.IsNullOrEmpty(line)) // to be on the safe side
.Select(line => line.Split('\t'))
.ToDictionary(items => items[0], items => items[1]);
如果您坚持使用StreamReader
,您可以为循环实现一个简单的:
// Do not forget to Dispose it
using (StreamReader sr = new StreamReader(@"N:\Desktop\krew.txt")) {
// if fullLine == null, sr is at the end and can't read any more lines
for (string fullLine = sr.ReadLine(); fullLine != null; fullLine = sr.ReadLine()) {
string[] wholeLine = fullLine.Split('\t');
dict.Add(wholeLine[0], wholeLine[1]);
}
}
您最好这样编写循环:
while (true)
{
string fullLine = sr.ReadLine();
if (fullLine == null)
break;
string[] wholeLine = fullLine.Split('\t');
dict.Add(wholeLine[0], wholeLine[1]);
...
正如您现在编写的循环一样,fullLine
将在文件末尾变为null,然后fullLine.Split('\t');
将抛出一个NullReferenceException
我怀疑这就是你说的第一行是空的,它没有进一步的
。它实际上不可能是导致问题的第一行,因为你将全文
初始化为“”,但我认为这是根本的问题。使用.Peek()方法读取到文件的结尾。它应该超过第一行,因为第一行是空的
- Null与“”不是一回事
- Peek()将检查下一个字符,如果该行为
空白
,它将返回”
>P>如果在第2行到第X行,则空行不为空,因此,PEEK()将返回- 1。检查其上的文档。
.Peek()
像这样更改代码
while ((fullLine = sr.ReadLine()) != null)
{
string[] wholeLine = fullLine.Split('\t');
dict.Add(wholeLine[0], wholeLine[1]);
}
尝试使用下面的命令。这将为文件中的每一行设置行变量
StreamReader sr = new StreamReader(@"N:\Desktop\krew.txt");
Dictionary<string, string> dict = new Dictionary<string, string>();
string line;
while ((line = sr.ReadLine()) != null)
{
string[] wholeLine = line.Split('\t');
dict.Add(wholeLine[0], wholeLine[1]);
}
StreamReader sr=newstreamreader(@“N:\Desktop\krew.txt”);
Dictionary dict=新字典();
弦线;
而((line=sr.ReadLine())!=null)
{
string[]wholeLine=line.Split('\t');
dict.Add(整行[0],整行[1]);
}
如果您不完全依赖于必须使用StreamReader
string[]read=File.ReadAllLines(文件路径);
for(int i=0;i
Uh,第一次运行时fullLine
怎么可能为空?您在前一行中将其设置为空字符串,该字符串不为空。ReadLine()
仅当到达文件末尾时才返回null。您的文件包含内容,并且您没有键入路径?您的程序可以访问不同的驱动器号?使用file.ReadAllLines(..)可能更容易
而不是StreamReader,如果您打算继续使用StreamReader,请查看,它使用而使用。a.txt的行之间不能有真正的“null”值。如果我错了,请纠正我,但它们不是用StreamReader以/n的形式读取的吗?@xTwisteDx:文件不能有null
行,而是空的“”
ones@xTwisteDxAs状态:如果到达输入流的结尾,则返回值为null,因此循环检测EoF条件的方式就是这样。为什么分配fullLine=sr.ReadLine()时不起作用;如果没有它,它会做什么?@Wurfv_uu必须在while循环条件中对变量进行赋值。这样,它会在检查是否为null之前设置变量
while ((fullLine = sr.ReadLine()) != null)
{
string[] wholeLine = fullLine.Split('\t');
dict.Add(wholeLine[0], wholeLine[1]);
}
StreamReader sr = new StreamReader(@"N:\Desktop\krew.txt");
Dictionary<string, string> dict = new Dictionary<string, string>();
string line;
while ((line = sr.ReadLine()) != null)
{
string[] wholeLine = line.Split('\t');
dict.Add(wholeLine[0], wholeLine[1]);
}