如何从文件中读取和写入? [英] How do I read and write from a file?

查看:169
本文介绍了如何从文件中读取和写入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将以下信息写入文件.....流文件,,,,,,

i want to write the following information to a file.....a stream file,,,,,,

Console.WriteLine("________________Student information______________");
                   Console.WriteLine("Enter student name ");
                   Firstname = Console.ReadLine();
                   Console.WriteLine("Enter the Surname ");
                   Surname = Console.ReadLine();
                   Console.WriteLine("Enter student date of birth");
                   DOB = DateTime.Parse(Console.ReadLine());
                   Console.WriteLine("Enter student Gender either F/M ");
                   Gender = char.Parse(Console.ReadLine());
                   Console.WriteLine("Total fees paid N$");
                   Total_Fees_paid = double.Parse(Console.ReadLine());
                   amount_owed = totalcost - Total_Fees_paid;
                   Console.WriteLine("__________NEXT OF KIN__________");
                   Console.WriteLine("Enter next of kin/parent/gurdian's Title ");
                   nTitle = Console.ReadLine();
                   Console.WriteLine("Enter next of kin/parent/gurdian's Name ");
                   nFirstname = Console.ReadLine();
                   Console.WriteLine("Enter next of kin/parent/gurdian's Surname ");
                   nsurname = Console.ReadLine();
                   Console.WriteLine("Enter next of kin/parent/gurdian's date of birth");
                   nDOB = DateTime.Parse(Console.ReadLine());
                   Console.WriteLine("Enter next of kin/parent/gurdian's Occupation");
                   nOccupation = Console.ReadLine();
                   Console.WriteLine("Enter next of kin/parent/gurdian's Desgnation");
                   nDesgination = Console.ReadLine();





我尝试过:



Streamwriter wr = new Streamwriter;



What I have tried:

Streamwriter wr = new Streamwriter;

推荐答案

);
Total_Fees_paid = < span class =code-keyword> double .Parse(Console.ReadLine());
amount_owed = totalcost - Total_Fees_paid;
Console.WriteLine( __________ NEIN OF KIN __________);
Console.WriteLine( 输入kin / parent / gurdian的标题);
nTitle = Console.ReadLine();
Console.WriteLine( 输入kin / parent / gurdian的名称);
nFirstname = Console.ReadLine();
Co nsole.WriteLine( 输入kin / parent / gurdian的下一个姓氏);
nsurname = Console.ReadLine();
Console.WriteLine( 输入亲属/父母/ gurdian出生日期的下一个) ;
nDOB = DateTime.Parse(Console.ReadLine());
Console.WriteLine( 输入kin / parent / gurdian的职业);
nOccupation = Console.ReadLine();
Console.WriteLine( 输入kin / parent / gurdian的下一个Desgnation);
nDesgination = Console.ReadLine();
"); Total_Fees_paid = double.Parse(Console.ReadLine()); amount_owed = totalcost - Total_Fees_paid; Console.WriteLine("__________NEXT OF KIN__________"); Console.WriteLine("Enter next of kin/parent/gurdian's Title "); nTitle = Console.ReadLine(); Console.WriteLine("Enter next of kin/parent/gurdian's Name "); nFirstname = Console.ReadLine(); Console.WriteLine("Enter next of kin/parent/gurdian's Surname "); nsurname = Console.ReadLine(); Console.WriteLine("Enter next of kin/parent/gurdian's date of birth"); nDOB = DateTime.Parse(Console.ReadLine()); Console.WriteLine("Enter next of kin/parent/gurdian's Occupation"); nOccupation = Console.ReadLine(); Console.WriteLine("Enter next of kin/parent/gurdian's Desgnation"); nDesgination = Console.ReadLine();





我尝试过:



Streamwriter wr = new Streamwriter;



What I have tried:

Streamwriter wr = new Streamwriter;


如何:将文本写入文件 [ ^ ]



但是你最好存放具有保存输入数据的属性的类中的数据,并序列化该类的类。



基本序列化 [ ^ ]


StreamWriter wr = new StreamWriter(@c:\ file.txt);

wr.WriteLine(FirstName);

wr.WriteLine(Surname);

......等其余部分...

wr.Close();





如果你想每次附加到文件,那么使用:StreamWriter wr = new StreamWriter(@c:\ file.txt,true);
StreamWriter wr = new StreamWriter(@"c:\file.txt");
wr.WriteLine(FirstName);
wr.WriteLine(Surname);
...etc for the rest of the lines...
wr.Close();


If you want to append to the file everytime then use: StreamWriter wr = new StreamWriter(@"c:\file.txt", true);


这篇关于如何从文件中读取和写入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆