使用C#中的列表编辑文件
本文关键字:列表 编辑文件 使用 | 更新日期: 2023-09-27 18:22:07
我试图自学如何在C#中编辑文件,但遇到了一个问题。我试图允许用户打开一个文件,其中包含一句话:
"How are you today, <name>?"
我有一节课要阅读文件并将句子添加到列表中。这是我为此编写的代码:
class FileReader
{
public string reader(string sentence)
{
string f = sentence;
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
lines.Add(line);
}
return lines.ToString();
}
}
}
一旦它作为字符串返回,我就有了另一个类,它将读取列表,然后向用户询问名称。然后,它将用用户输入的名称替换<name>
。以下是我尝试编写的处理此问题的代码:
class Asker
{
public string asker(string sentence)
{
List<string> lines = new List<string>();
Console.Write("Enter Name: ");
string name = Console.ReadLine();
string text = File.ReadAllText(sentence);
text = text.Replace("<name>", name);
lines.Add(text);
File.WriteAllText(sentence, text);
return lines.ToString();
}
}
在这个类结束时,Asker类应该返回一个包含新句子的列表,其中name
现在取代了原始句子中的<name>
。总的来说,每次我尝试运行它时都会收到一个错误代码
static void Main(string[] args)
{
Console.Write("Enter the name of the story file: ");
string filename = Console.ReadLine();
FileReader read = new FileReader();
string uneditedSentence = read.reader(filename);
Asker ask = new Asker();
string newSentence = ask.asker(uneditedSentence);
Console.WriteLine(newSentence);
}
当我运行这个程序时,我收到一条消息,它停止工作,只是崩溃了。
您的代码中很少需要编辑:
1) 当您返回行.toString()>>时,它将返回System.Collections.Generic.List`1[System.String],而不是文件中的文本。所以你应该划线,而不是划线。字符串()。所以函数的返回类型应该是List。或者,如果你想返回字符串,那么代码应该是:
class FileReader
{
public string reader(string sentence)
{
string f = sentence;
string lines = "";
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
lines +=line+ Environment.NewLine;
}
return lines;
}
}
}
2) 在你的asker类中:asker函数参数是文件的内容,而不是文件名:
string text = File.ReadAllText(sentence)
因此上述代码将不起作用。
更好的方法:您不需要文件读取器类:
class Program
{
static void Main(string[] args)
{
Console.Write("Enter the name of the story file: ");
string filename = Console.ReadLine();
Asker ask = new Asker();
string newSentence = ask.asker(filename);
Console.WriteLine(newSentence);
string name = Console.ReadLine();
}
}
class Asker
{
public string asker(string sentence)
{
Console.Write("Enter Name: ");
string name = Console.ReadLine();
string text = File.ReadAllText(sentence);
text = text.Replace("<name>", name);
File.WriteAllText(sentence, text);
return text;
}
}
reader
方法接受filename作为唯一参数,并返回文件的内容。
这意味着在这行代码中uneditedSentence
是文件filename
:的内容
string uneditedSentence = read.reader(filename);
然后,您将此uneditedSentence
传递给您的asker
:
string newSentence = ask.asker(uneditedSentence);
同时,您的asker
方法有以下行:
string text = File.ReadAllText(sentence);
File.WriteAllText(sentence, text);
它需要文件路径,而您提供其内容。
它提供了一个错误,因为您提供的文件路径不正确。原因很简单——命名。你对变量的命名不正确,会感到困惑。
请参阅有关ReadAllText和WriteAllText的MSDN文档。
此外,它不会显示正确的结果,因为您将ToString()
应用于List<string>
,期望它将转换为单个字符串。然而,它只会产生类似System.Collections.Generic.List'1[System.String]
的结果。
以下是您应该如何做到这一点:
class FileReader // Actually, it is a useless class, get rid of it
{
public string Read(string filename)
{
return File.ReadAllText(filename);
}
}