Streamreader不会在文本文件中读取数据C# [英] Streamreader doesn't read data in text file C#
问题描述
我有两个类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 WriteMethod()
{
int count = 0;
while(count< 2)
{
Console.Write(输入您的姓名:);
Name = Console.ReadLine();
Console.Write(输入您的ID:);
ID = int.Parse(Console.ReadLine());
Console.Write(输入您的年龄:);
Age = int.Parse(Console.ReadLine());
Console.Write(输入您的电子邮件:);
Email = Console.ReadLine();
StreamWriter Sw = new StreamWriter(fileone.txt,true);
string output = string.Format(谢谢注册!您提交的信息是:+ Environment.NewLine +名称:{0}
+ Environment.NewLine +ID:{1} + Environment.NewLine +年龄:{2}+ Environment.NewLine +电子邮件:{3},姓名,ID,年龄,电子邮件);
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();
}
}
非常感谢。等待您的答案。
看来您还没有设置变量 $ c>。您已设置
行
变量。
public static void ReadingMethod b $ b {
FileStream fsr = new FileStream(fileone.txt,FileMode.Open,FileAccess.Read);
StreamReader Sr = new StreamReader(fsr);
string line = Sr.ReadToEnd();
Console.WriteLine( - Reading the File--+ Environment.NewLine + line + Environment.NewLine);
Console.ReadLine();
Sr.Close();
fsr.Close();
}
我修改的内容从 code>到
行
。
希望它有帮助。
I have two classes "allmethods.cs" and "caller.cs"
I have two methods in the "allmethods.cs" class which are "WritingMethod" and "ReadingMethod"
The program should write and read from a text file. It writes smoothly when I call the "WritingMethod" but When I call the "ReadingMethod" it shows null as if there is no data in the text file.
I can't identify the problem in my code, I'd be glad if anyone help me identify the problem.
Here is my code:
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();
}
}
Thank you very much. Waiting for your answers.
It seems that you have not set the variable output
. You have set line
variable.
public static void ReadingMethod()
{
FileStream fsr = new FileStream("fileone.txt", FileMode.Open, FileAccess.Read);
StreamReader Sr = new StreamReader(fsr);
string line = Sr.ReadToEnd();
Console.WriteLine("--Reading The File--" + Environment.NewLine + line + Environment.NewLine);
Console.ReadLine();
Sr.Close();
fsr.Close();
}
What I have modified is changed from output
to line
.
Hope it helps.
这篇关于Streamreader不会在文本文件中读取数据C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!