logo

Ako čítať a písať textový súbor v C #?

Ukončenie programu vedie k vymazaniu všetkých údajov, ktoré s ním súvisia. Preto musíme dáta niekde uložiť. Súbory sa používajú na trvalé ukladanie a zdieľanie údajov. C# možno použiť na získavanie a manipuláciu s údajmi uloženými v textových súboroch.

Čítanie textového súboru: Trieda súboru v C# definuje dve statické metódy na čítanie textového súboru File.ReadAllText() a File.ReadAllLines() .



  • File.ReadAllText() prečíta celý súbor naraz a vráti reťazec. Tento reťazec musíme uložiť do premennej a použiť ho na zobrazenie obsahu na obrazovke.
  • File.ReadAllLines() prečíta súbor po jednom riadku a vráti tento riadok vo formáte reťazca. Na uloženie každého riadku potrebujeme pole reťazcov. Obsah súboru zobrazíme pomocou rovnakého poľa reťazcov.

Existuje ďalší spôsob, ako čítať súbor, a to pomocou objektu StreamReader. StreamReader tiež číta jeden riadok po druhom a vracia reťazec. Všetky vyššie uvedené spôsoby čítania súboru sú znázornené v príklade kódu nižšie.

môj živý kriket








// 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();> >}> }>

>

>

Ak chcete spustiť tento program, uložte súbor s .cs rozšírenie a potom môžete spustiť pomocou csc názov súboru.cs príkaz na cmd. Alebo môžete použiť Visual Studio . Tu máme textový súbor s názvom ako Textfile.txt ktoré majú obsah zobrazený vo výstupe.

Výkon:

čítanie textového súboru v C#

Písanie textového súboru: Trieda File v C# definuje dve statické metódy na písanie textového súboru File.WriteAllText() a File.WriteAllLines() .

  • File.WriteAllText() zapíše celý súbor naraz. Vyžaduje si to dva argumenty, cestu k súboru a text, ktorý treba zapísať.
  • File.WriteAllLines() zapisuje súbor po jednom riadku. Vyžaduje dva argumenty, cestu k súboru a text, ktorý sa má zapísať, čo je pole reťazcov.

Existuje ďalší spôsob, ako zapisovať do súboru, a to pomocou objektu StreamWriter. StreamWriter tiež zapisuje jeden riadok naraz. Všetky tri spôsoby zápisu vytvoria nový súbor, ak súbor neexistuje, ale ak sa súbor už nachádza na danom mieste, prepíše sa. Všetky vyššie uvedené spôsoby zapisovania do textového súboru sú znázornené v príklade kódu nižšie.


indická herečka rani mukerji



// 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();> >}> }>

>

substring_index v sql

>

Ak chcete spustiť tento program, uložte súbor s .cs rozšírenie a potom môžete spustiť pomocou csc názov súboru.cs príkaz na cmd. Alebo môžete použiť Visual Studio .

Výkon:

písanie súboru v C#

V prípade, že chcete do existujúceho súboru pridať ďalší text bez prepísania údajov, ktoré už sú v ňom uložené, môžete použiť metódy pripojenia, ktoré poskytuje trieda File System.IO.




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();> >}> }>

>

>

Výkon:

pridanie textu do súboru v C#

objekt java