我有两个类“allmethods.cs”和“caller.cs”
我在“allmethods.cs”类中有两个方法,分别是“WritingMethod”和“ReadingMethod”
程序应该从文本文件中写入和读取。当我调用“WritingMethod”时它写得很顺利,但是当我调用“ReadingMethod”时它显示为空,就好像文本文件中没有数据一样。
我无法识别代码中的问题,如果有人帮助我识别问题,我会很高兴。
这是我的代码:
public class allmethods
{
private static string Name;
private static int ID;
private static int Age;
private static string Email;
private static string output;
public static void WritingMethod()
{
int count = 0;
while (count < 2)
{
Console.Write(" Enter your Name: ");
Name = Console.ReadLine();
Console.Write(" Enter your ID: ");
ID = int.Parse(Console.ReadLine());
Console.Write(" Enter your Age: ");
Age = int.Parse(Console.ReadLine());
Console.Write(" Enter your E-mail: ");
Email = Console.ReadLine();
StreamWriter Sw = new StreamWriter("fileone.txt", true);
string output = string.Format("Thank you for registration! Your Submitted information are:" + Environment.NewLine + "Name: {0}"
+ Environment.NewLine + "ID: {1}" + Environment.NewLine + "Age: {2}" + Environment.NewLine + "E-mail: {3}", Name, ID, Age, Email);
Console.WriteLine(output);
Sw.WriteLine(output + Environment.NewLine);
Console.ReadLine();
Sw.Close();
count++;
}
}
public static void ReadingMethod()
{
FileStream fsr = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read);
StreamReader Sr = new StreamReader(fsr);
string line = Sr.ReadLine();
Console.WriteLine("--Reading The File--" + Environment.NewLine + output + Environment.NewLine);
Console.ReadLine();
Sr.Close();
fsr.Close();
}
}
非常感谢。等待您的答复。