C# 0 The Complete Reference



Download 4,07 Mb.
Pdf ko'rish
bet489/1096
Sana23.01.2022
Hajmi4,07 Mb.
#402171
1   ...   485   486   487   488   489   490   491   492   ...   1096
Bog'liq
C-Sharp 3 The Complete Reference Herbert Schildt

Character-Based File I/O

Although byte-oriented file handling is quite common, it is possible to use character-based 

streams for this purpose. The advantage to the character streams is that they operate directly 

on Unicode characters. Thus, if you want to store Unicode text, the character streams are 

certainly your best option. In general, to perform character-based file operations, you will wrap 

a

FileStream

 inside either a 



StreamReader

 or a 


StreamWriter

. These classes automatically 

convert a byte stream into a character stream, and vice versa.

Remember, at the operating system level, a file consists of a set of bytes. Using a 



StreamReader

 or 


StreamWriter

 does not alter this fact.



StreamWriter

 is derived from 



TextWriter

.

StreamReader

 is derived from 

TextReader

.

Thus,



StreamWriter

 and 


StreamReader

 have access to the methods and properties defined 

by their base classes.

Using StreamWriter

To create a character-based output stream, wrap a 



Stream

 object (such as a 



FileStream

)

inside a 



StreamWriter

.

StreamWriter

 defines several constructors. One of its most popular 

is shown here:

StreamWriter(Stream 

stream

)

Here, 



stream

 is the name of an open stream. This constructor throws an 



ArgumentException

if

stream

 is not opened for output and an 

ArgumentNullException

 if 


stream

 is null. Once 

created, a 

StreamWriter

 automatically handles the conversion of characters to bytes.

Here is a simple key-to-disk utility that reads lines of text entered at the keyboard and 

writes them to a file called “test.txt.” Text is read until the user enters the word “stop”. It 

uses a 

FileStream

 wrapped in a 



StreamWriter

 to output to the file.

// A simple key-to-disk utility that demonstrates a StreamWriter.

using System;

using System.IO;

class KtoD {

  static void Main() {

    string str;

    FileStream fout;

    try {

      fout = new FileStream("test.txt", FileMode.Create);

    }


    catch(IOException exc) {

      Console.WriteLine("Cannot Open File");

      Console.WriteLine(exc.Message);

      return ;

    }

    StreamWriter fstr_out = new StreamWriter(fout);



www.freepdf-books.com


Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   485   486   487   488   489   490   491   492   ...   1096




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

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish