logo

Kā lasīt un rakstīt teksta failu C #?

Programmas darbības pārtraukšana noved pie visu ar to saistīto datu dzēšanas. Tāpēc mums kaut kur ir jāglabā dati. Faili tiek izmantoti datu pastāvīgai glabāšanai un koplietošanai. C# var izmantot, lai izgūtu un apstrādātu teksta failos saglabātos datus.

Teksta faila lasīšana: Failu klase C# definē divas statiskas metodes teksta faila lasīšanai File.ReadAllText() un File.ReadAllLines() .



  • File.ReadAllText() nolasa visu failu uzreiz un atgriež virkni. Mums šī virkne jāsaglabā mainīgajā un jāizmanto, lai ekrānā parādītu saturu.
  • File.ReadAllLines() nolasa failu pa vienai rindai un atgriež šo rindiņu virknes formātā. Mums ir nepieciešams virkņu masīvs, lai saglabātu katru rindiņu. Mēs parādām faila saturu, izmantojot to pašu virkņu masīvu.

Ir vēl viens veids, kā lasīt failu, un tas ir, izmantojot StreamReader objektu. StreamReader arī nolasa vienu rindiņu vienlaikus un atgriež virkni. Visi iepriekš minētie faila lasīšanas veidi ir parādīti tālāk sniegtajā koda piemērā.








// C# program to illustrate how> // to read a file in C#> using> System;> using> System.IO;> > class> Program {> >static> void> Main(>string>[] args)> >{> >// Store the path of the textfile in your system> >string> file =>@'M:DocumentsTextfile.txt'>;> > >Console.WriteLine(>'Reading File using File.ReadAllText()'>);> > >// To read the entire file at once> >if> (File.Exists(file)) {> >// Read all the content in one string> >// and display the string> >string> str = File.ReadAllText(file);> >Console.WriteLine(str);> >}> >Console.WriteLine();> > >Console.WriteLine(>'Reading File using File.ReadAllLines()'>);> > >// To read a text file line by line> >if> (File.Exists(file)) {> >// Store each line in array of strings> >string>[] lines = File.ReadAllLines(file);> > >foreach>(>string> ln>in> lines)> >Console.WriteLine(ln);> >}> >Console.WriteLine();> > >Console.WriteLine(>'Reading File using StreamReader'>);> > >// By using StreamReader> >if> (File.Exists(file)) {> >// Reads file line by line> >StreamReader Textfile =>new> StreamReader(file);> >string> line;> > >while> ((line = Textfile.ReadLine()) !=>null>) {> >Console.WriteLine(line);> >}> > >Textfile.Close();> > >Console.ReadKey();> >}> >Console.WriteLine();> >}> }>

>

>

Lai palaistu šo programmu, saglabājiet failu ar .cs paplašinājumu un pēc tam var izpildīt, izmantojot csc faila nosaukums.cs komanda cmd. Vai arī varat izmantot Visual Studio. Šeit mums ir teksta fails ar nosaukumu kā Teksta fails.txt kuru saturs ir parādīts izvadē.

Izvade:

teksta faila lasīšana C#

Teksta faila rakstīšana: Failu klase C # definē divas statiskas metodes teksta faila rakstīšanai File.WriteAllText() un File.WriteAllLines() .

  • File.WriteAllText() ieraksta visu failu uzreiz. Tam nepieciešami divi argumenti: faila ceļš un rakstāmais teksts.
  • File.WriteAllLines() ieraksta failu pa vienai rindai. Tam nepieciešami divi argumenti: faila ceļš un rakstāmais teksts, kas ir virkņu masīvs.

Ir vēl viens veids, kā rakstīt failā, un tas ir, izmantojot StreamWriter objektu. StreamWriter arī raksta vienu rindiņu vienlaikus. Visas trīs rakstīšanas metodes izveido jaunu failu, ja fails neeksistē, bet, ja fails jau atrodas norādītajā vietā, tas tiek pārrakstīts. Visi iepriekš minētie veidi, kā rakstīt teksta failā, ir ilustrēti tālāk sniegtajā koda piemērā.


c++ int uz virkni



// C# program to illustrate how> // to write a file in C#> using> System;> using> System.IO;> > class> Program {> >static> void> Main(>string>[] args)> >{> >// Store the path of the textfile in your system> >string> file =>@'M:DocumentsTextfile.txt'>;> > >// To write all of the text to the file> >string> text =>'This is some text.'>;> >File.WriteAllText(file, text);> > >// To display current contents of the file> >Console.WriteLine(File.ReadAllText(file));> >Console.WriteLine();> > >// To write text to file line by line> >string>[] textLines1 = {>'This is the first line'>,> >'This is the second line'>,> >'This is the third line'> };> > >File.WriteAllLines(file, textLines1);> > >// To display current contents of the file> >Console.WriteLine(File.ReadAllText(file));> > >// To write to a file using StreamWriter> >// Writes line by line> >string>[] textLines2 = {>'This is the new first line'>,> >'This is the new second line'> };> > >using>(StreamWriter writer =>new> StreamWriter(file))> >{> >foreach>(>string> ln>in> textLines2)> >{> >writer.WriteLine(ln);> >}> >}> >// To display current contents of the file> >Console.WriteLine(File.ReadAllText(file));> > >Console.ReadKey();> >}> }>

>

>

Lai palaistu šo programmu, saglabājiet failu ar .cs paplašinājumu un pēc tam var izpildīt, izmantojot csc faila nosaukums.cs komanda cmd. Vai arī varat izmantot Visual Studio.

Izvade:

rakstot failu C#

kā izdrukāt java

Ja vēlaties esošam failam pievienot vairāk teksta, nepārrakstot tajā jau saglabātos datus, varat izmantot pievienošanas metodes, ko nodrošina System.IO failu klase.




using> System;> using> System.IO;> > class> Program {> >static> void> Main(>string>[] args)> >{> >// Store the path of the textfile in your system> >string> file =>@'M:DocumentsTextfile.txt'>;> > >// To write all of the text to the file> >string> text1 =>'This is some text.'>;> >File.WriteAllText(file, text1);> > >// To append text to a file> >string> text2 =>'This is text to be appended'>;> >File.AppendAllText(file, text2);> > >// To display current contents of the file> >Console.WriteLine(File.ReadAllText(file));> >Console.ReadKey();> >}> }>

>

>

Izvade:

teksta pievienošana failam C#