C #. net-da matnli fayllar bilan ishlash



Download 88.34 Kb.
bet1/2
Sana11.01.2020
Hajmi88.34 Kb.
  1   2
C # .NET-da matnli fayllar bilan ishlash
Xarajatlarni sizning uyingiz farovonligidan to'lash uchun sizga hamma narsani o'rgatamiz.

Ko'proq ma'lumotga ega bo'lish ...


Oldingi darsda, fayllar bilan ishlashga kirish, biz Windows operatsion tizimlarida yozish imtiyozlari bilan tanishdik. Ilova ma'lumotlarini qattiq diskda saqlashning eng oddiy usuli bu matnli fayllardan foydalanish. Sizlarning barchangiz .txt kengaytmasi bilan fayllarga duch kelganingizga aminman. Matn shunchaki bir nechta satrlarda saqlanadi. Chiziqlar, afsuski, OSga xos bo'lgan maxsus belgilar bilan ajratilgan. Yaxshiyamki, C # biz uchun bu muammoni hal qiladi.

Izoh: Internetdagi turli xil loyihalarda fayllarga yozish uchun turli xil sinflar va yondashuvlardan foydalangan holda duch kelishingiz mumkin. .NET doirasi vaqt o'tishi bilan ishlab chiqilgan va oldinga moslashuv maqsadida eski va yanada murakkab tuzilmalarni taqdim etadi. Men sizga eng oddiy va eng yangi yondashuvlarni ko'rsataman.


Writing text to a new file


First, let's create a new text file and write something into it. Create a new project (a console application) and name it TextFiles. The .NET framework provides the StreamWriter class for us which is used to write into text files.

Now, go ahead and add using System.IO into the source code. Next, we'll create a using block and create a new StreamWriter instance inside. As we already know from previous lessonsusing will automatically close the file once the reading/writing is finished. We'll specify the path to our file in the constructor:



using (StreamWriter sw = new StreamWriter(@"file.txt"))

{

}



Our StreamWriter now points to the appropriate file. We write a new line in text files using the WriteLine() method. Once we're done writing, we'll have to call the Flush() method, which takes care of emptying the buffer. We won't have to deal with any of that, all we have to remember is that any written lines may remain in the buffer memory for a while, so we force to write them using the Flush() method.

At this point, the code looks something like this:



using (StreamWriter sw = new StreamWriter(@"file.txt"))

{

sw.WriteLine("The first line");



sw.WriteLine("This text is on the second line");

sw.WriteLine("And the third one.");

sw.Flush();

}

Once we run it, a file named file.txt will be created in our project folder, specifically, in the bin/debug folder. We have already gone over how to handle file path and writing privileges in C# .NET correctly, so we won't deal with any of that here so as to keep the code simple. As you can see, the file now exists and contains the text we designated:





Download 88.34 Kb.

Do'stlaringiz bilan baham:
  1   2




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2020
ma'muriyatiga murojaat qiling

    Bosh sahifa
davlat universiteti
ta’lim vazirligi
O’zbekiston respublikasi
maxsus ta’lim
zbekiston respublikasi
o’rta maxsus
davlat pedagogika
axborot texnologiyalari
nomidagi toshkent
pedagogika instituti
texnologiyalari universiteti
navoiy nomidagi
samarqand davlat
guruh talabasi
ta’limi vazirligi
nomidagi samarqand
toshkent axborot
toshkent davlat
haqida tushuncha
Darsning maqsadi
xorazmiy nomidagi
Toshkent davlat
vazirligi toshkent
tashkil etish
Alisher navoiy
Ўзбекистон республикаси
rivojlantirish vazirligi
matematika fakulteti
pedagogika universiteti
таълим вазирлиги
sinflar uchun
Nizomiy nomidagi
tibbiyot akademiyasi
maxsus ta'lim
ta'lim vazirligi
махсус таълим
bilan ishlash
o’rta ta’lim
fanlar fakulteti
Referat mavzu
Navoiy davlat
umumiy o’rta
haqida umumiy
Buxoro davlat
fanining predmeti
fizika matematika
universiteti fizika
malakasini oshirish
kommunikatsiyalarini rivojlantirish
davlat sharqshunoslik
jizzax davlat