// StreamReader ob'ektini oling.
FileInfo f5 = yangi FileInfo (@ "C: \ boot.ini");
StreamReader sreader = f5. OpenText ();
// StreamReader ob'ektidan foydalaning ...
sreader.Close ();
}
Yaqinda ko'rib turganingizdek, StreamReader turi belgilar ma'lumotlarini o'qish usulini taqdim etadi
asosiy fayl.
FileInfo.CreateText () va FileInfo.AppendText () usullari. Ushbu nuqtada qiziqishning so'nggi ikkita usuli - bu CreateText () va AppendText () , ikkalasi ham
bu erda ko'rsatilganidek, StreamWriter ma'lumotlarini qaytaring :
Static using Main (string [] args)
{
...
FileInfo f6 = yangi FileInfo (@ "C: \ Test5.txt");
StreamWriter yozuvchisi = f6.CreateText ();
// StreamWriter ob'ektidan foydalaning ...
swriter.Close ();
FileInfo f7 = yangi FileInfo (@ "C: \ FinalTest.txt");
StreamWriter swriterAppend = f7.AppendText ();
// StreamWriter ob'ektidan foydalaning ...
swriterAppend.Close ();
}
Siz taxmin qilganingizdek, StreamWriter turi ostiga belgi ma'lumotlarini yozish usulini taqdim etadi yolg'on fayl.
Fayl turi bilan ishlash
Fayl turi bilan deyarli bir xil funktsional beradi FileInfo bir qator foydalanib, turi statik a'zolar. Kabi FileInfo , Fayl yetkazib AppendText () , yaratish () , CreateText () , Open () , OpenRead () , OpenWrite () va OpenText () usullari. Aslida, ko'p hollarda, File va FileStream turlari bir-birining o'rnida ishlatilishi mumkin. Tasvirlash uchun avvalgi FileStream misollarining har biri bo'lishi mumkin o'rniga Fayl turini ishlatib soddalashtirilgan :
Static void Main (string [] args)
{
// File.Create () orqali FileStream ob'ektini oling.
FileStream fs = File.Create (@ "C: \ Test.dat");
fs.Close ();
// File.Open () orqali FileStream ob'ektini oling.
FileStream fs2 = File.Open (@ "C: \ Test2.dat",
FileMode.OpenOrCreate,
FileAccess.ReadWrite, FileShare.None);
fs2. Close ();
// Faqat o'qish uchun ruxsat berilgan FileStream ob'ektini oling.FileStream readOnlyStream = File.OpenRead (@ "Test3.dat");
readOnlyStream.Close ();
// Faqat yozish uchun ruxsat berilgan FileStream ob'ektini oling. FileStream writeOnlyStream = File.OpenWrite (@ "Test4.dat"); writeOnlyStream.Close ();
// StreamReader ob'ektini oling.
StreamReader sreader = File.OpenText (@ "C: \ boot.ini");
sreader.Close ();
// StreamWriters-ni oling.
StreamWriter swriter = File.CreateText (@ "C: \ Test3.txt");
swriter.Close ();
StreamWriter swriterAppend = File.AppendText (@ "C: \ FinalTest.txt");
swriterAppend.Close ();
}
New .NET 2.0 fayl a'zolari
FileInfodan farqli o'laroq , Fayl turi 16-5-jadvalda ko'rsatilgan bir nechta noyob a'zolarni (.NET 2.0 dan boshlab) qo'llab-quvvatlaydi,
bu matnli ma'lumotlarni o'qish va yozish jarayonlarini ancha soddalashtirishi mumkin.
Do'stlaringiz bilan baham: |