Prekinitev programa povzroči izbris vseh podatkov, povezanih z njim. Zato moramo podatke nekam shraniti. Datoteke se uporabljajo za trajno shranjevanje in skupno rabo podatkov. C# se lahko uporablja za pridobivanje in obdelavo podatkov, shranjenih v besedilnih datotekah.
Branje besedilne datoteke: Datotečni razred v C# definira dve statični metodi za branje besedilne datoteke File.ReadAllText() in File.ReadAllLines() .
- File.ReadAllText() prebere celotno datoteko hkrati in vrne niz. Ta niz moramo shraniti v spremenljivko in ga uporabiti za prikaz vsebine na zaslonu.
- File.ReadAllLines() bere datoteko eno vrstico naenkrat in vrne to vrstico v obliki niza. Za shranjevanje vsake vrstice potrebujemo niz nizov. Vsebino datoteke prikažemo z uporabo istega polja nizov.
Obstaja še en način za branje datoteke in to z uporabo predmeta StreamReader. StreamReader bere tudi eno vrstico naenkrat in vrne niz. Vsi zgoraj omenjeni načini branja datoteke so prikazani v spodnji vzorčni kodi.
// 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();> >}> }> |
>
if in else v bash
>
Če želite zagnati ta program, shranite datoteko z .cs razširitev in nato lahko izvedete z uporabo csc ime datoteke.cs ukaz na cmd. Lahko pa uporabite Visual Studio. Tukaj imamo besedilno datoteko z imenom Textfile.txt ki imajo vsebino prikazano v izhodu.
Izhod:

Pisanje besedilne datoteke: Razred File v C# definira dve statični metodi za pisanje besedilne datoteke in sicer File.WriteAllText() in File.WriteAllLines() .
- File.WriteAllText() zapiše celotno datoteko hkrati. Potrebuje dva argumenta, pot do datoteke in besedilo, ki ga je treba napisati.
- File.WriteAllLines() zapiše datoteko eno vrstico naenkrat. Potrebuje dva argumenta, pot do datoteke in besedilo, ki ga je treba zapisati, ki je niz nizov.
Obstaja še en način za pisanje v datoteko in to je uporaba predmeta StreamWriter. StreamWriter piše tudi eno vrstico naenkrat. Vsi trije načini pisanja ustvarijo novo datoteko, če datoteka ne obstaja, če pa je datoteka že prisotna na tem določenem mestu, je prepisana. Vsi zgoraj omenjeni načini pisanja v besedilno datoteko so prikazani v spodnji vzorčni kodi.
// 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();> >}> }> |
>
>
Če želite zagnati ta program, shranite datoteko z .cs razširitev in nato lahko izvedete z uporabo csc ime datoteke.cs ukaz na cmd. Lahko pa uporabite Visual Studio.
Izhod:

razvrsti matriko java
Če želite v obstoječo datoteko dodati več besedila, ne da bi prepisali podatke, ki so že shranjeni v njej, lahko uporabite metode dodajanja, ki jih ponuja razred datoteke 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();> >}> }> |
>
>
Izhod:
