【发布时间】:2013-04-20 02:04:09
【问题描述】:
我有两个类“allmethods.cs”和“caller.cs”
我在“allmethods.cs”类中有两个方法,分别是“WritingMethod”和“ReadingMethod”
程序应该读写一个文本文件。当我调用“WritingMethod”时它可以顺利写入,但是当我调用“ReadingMethod”时它显示为 null,就好像文本文件中没有数据一样。
我无法识别代码中的问题,如果有人帮助我识别问题,我会很高兴。
这是我的代码:
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();
}
}
非常感谢。等待您的答复。
【问题讨论】:
-
ReadingMethod 中有输出而不是行
标签: c# class streamreader readline