我的班级在这里有一段代码,我在其中输入要写入.txt和.bin文件的值。
我的问题是我的.txt文件最终为空,而我的.bin文件未以二进制形式写入。
我相信我已经正确编写并关闭了.bin文件和.txt,但是输入没有正确存储
这是我的代码
static void Main(string[] args)
{
string numberOfStudents, studentName, studentHeight, studentWeight;
int studentInput = 0, height;
bool numberOutput, heightOutput, weightOutput;
double weight;
Console.Write("Enter number of students: ");
numberOfStudents = Console.ReadLine();
numberOutput = int.TryParse(numberOfStudents, out studentInput);
if (numberOutput == true)
{
if (studentInput <= 0)
{
Console.WriteLine("Number of students must be a positive integer (greater than 0)!");
Console.ReadKey();
}
else
{
for (int i = 1; i <= studentInput; i++)
{
Console.Write("Enter student name: ");
studentName = Console.ReadLine();
Console.Write("Enter student height in centimetres: ");
studentHeight = Console.ReadLine();
heightOutput = int.TryParse(studentHeight, out height);
Console.Write("Enter student weight in kilograms: ");
studentWeight = Console.ReadLine();
weightOutput = double.TryParse(studentWeight, out weight);
try
{
StreamWriter outputFile;
outputFile = new StreamWriter("test.txt");
outputFile.Write(numberOfStudents + studentName + studentHeight + studentWeight);
outputFile.Close();
}
catch (System.IO.IOException exc)
{
Console.WriteLine("There was an error!: " + exc.Message);
}
try
{
FileStream outputFile = new FileStream("outFile.bin", FileMode.Create);
BinaryWriter BinFile = new BinaryWriter(outputFile);
BinFile.Write(studentName + " " + studentHeight + " " + studentWeight);
BinFile.Close();
}
catch (System.IO.IOException exc)
{
Console.WriteLine("There was an error!: " + exc.Message);
}
FileStream dataOutput = new FileStream("Database", FileMode.Create);
BinaryWriter databaseFile = new BinaryWriter(dataOutput);
StreamWriter textOutput = new StreamWriter("outText.txt");
databaseFile.Write(studentName + " " + studentHeight + " " + studentWeight);
databaseFile.Close();
textOutput.Close();
}
}
}
谢谢
您快要准备好了,但是代码存在一些问题,可以进行一些整理。
outFile.txt
,它被打开,然后在几行后关闭。查看test.txt
,它将具有最后的学生数据(请参阅下一点)。outputFile = new StreamWriter("test.txt", true)
此重载StreamWriter
将允许您选择追加到文件(true)还是覆盖(false)。using()
写入文件或任何实现的类时查看该语句IDisposable
。例如,打开StreamWriter时,您可以执行using (var outputFile = new StreamWriter("test.txt", true)) { }
。这确保了流写入器已被垃圾收集器关闭并处置(例如,您没有关闭dataOutput
文件流,如果文件流处于using(){}
块中,它将自动为您处理)try {} catch {}
块,每次写入文件时都不需要单独的try / catch。在Docs中,BinaryWriter
该类
将二进制类型的原始类型写入流,并支持以特定编码写入字符串。
因此,二进制写程序正在以二进制形式写,但是它所写的数据是字符串。它可以很容易地写int
,double
,bool
,等。
这是您的try / catch块中经过整理的代码(这只会创建test.txt和outFile.bin):
try
{
// will open or create file in 'append' mode
using (var outputFile = new StreamWriter("test.txt", true))
{
outputFile.Write($"{numberOfStudents}, {studentName}, {studentHeight},{studentWeight}");
outputFile.Write(Environment.NewLine);
}
using (BinaryWriter databaseFile = new BinaryWriter(File.Open("outFile.bin", FileMode.Append)))
{
databaseFile.Write(studentName + " " + studentHeight + " " + studentWeight);
}
}
catch (System.IO.IOException exc)
{
Console.WriteLine("There was an error!: " + exc.Message);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句