1- misol. uqish.txt fayldan ikkita sonni o’qib, ularni yig’indisini yozish.txt fayliga yozish dasturini ko’rib chiqamiz.
using System;
using System.Collections.Generic; using System.Linq;
using System.Text; using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
StreamWriter yozish = new StreamWriter("d:\\yozish.txt");
StreamReader uqish = new StreamReader("d:\\uqish.txt"); yozish.WriteLine(int.Parse(uqish.ReadLine()) + int.Parse(uqish.ReadLine()));
yozish.Close(); uqish.Close();
} } }
Dastur natijasi: dasturni ishlatishdan oldin uqish.txt faylda 2 ta son kiritish kerak, so’ng dastur ikkita sonni o’qib, ularni yig’indisini yozish.txt fayliga yozadi. Natijani ko’rish uchun yozish.txt faylni ochib ko’rish kerak
2-misol. fayl davomidan ma’lumotlarni yozish. class Program
{
static void Main(string[] args)
{
/*textFile.txt faylining yangi satridan "yangi satr" so'zini qo'shadi agar bu fayl bo'lmasa o'zi yaratib "yangi satr" so'zini qo'shadi*/ StreamWriter a;
a = File.AppendText("d:\\textFile.txt"); a.WriteLine("yangi satr");
a.Close();
} }
3- misol. fayldan ma’lumotlarni bitta bitta belgi orqali oqish va chiqarish. class Program
{
[STAThread]
static void Main(string[] args)
{
StreamReader uqish; try{
uqish = new StreamReader("d:\\textFile.txt");
}
catch {
Console.WriteLine("Faylni ochishda xatolik bor"); Console.ReadKey();
return;
}
int ch;
while ((ch = uqish.Read()) != (-1))
{
Console.WriteLine((char)ch);
}
Console.ReadKey();
} }
Dostları ilə paylaş: |