无法让程序读取文件,不知道我在这里做错了什么,欢迎任何建议。 尝试为地址簿执行项目,该文件包含所有人的姓名和地址以及电话号码。
public Form1()
{
string filename = "Addresses.txt";
ReadFile(filename);
ReadTokens(filename);
}//end of main
static void ReadFile(string filename)
{
StreamReader readFile;
readFile = File.OpenText(filename);
while (!readFile.EndOfStream)
{
Console.WriteLine(readFile.ReadLine());
}//end of while
{
readFile.Close();
Console.ReadKey();
}//End of read file
}
static void ReadTokens(string filename)
{
StreamReader readFile;
string line;
char[] delim = { ',' };
readFile = File.OpenText(filename);
while (!readFile.EndOfStream)
{
line = readFile.ReadLine(); // reads one line at a time
string[] tokens = line.Split(delim);
foreach (string str in tokens)
{
Console.WriteLine(str + "\t\t");
}//end of for each
}
Console.ReadKey();
readFile.Close();
}//end of ReadTokens
}//end of program
}//end namespace