C# 0 The Complete Reference


Close( )  works by calling  Dispose( )



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

Close( )

 works by calling 



Dispose( )

, which actually frees 

the resources.

N

OTE

N

OTE

The

using

 statement, described in Chapter 20, offers a way to automatically close a file when 

it is no longer needed. This approach is beneficial in many file-handling situations because it 

provides a simple means to ensure that a file is closed when it is no longer needed. However, to 

clearly illustrate the fundamentals of file handling, including the point at which a file can be 

closed, this chapter explicitly calls 

Close( )

 in all cases.

Reading Bytes from a FileStream

FileStream

 defines two methods that read bytes from a file: 



ReadByte( )

 and 


Read( )

. To 


read a single byte from a file, use 

ReadByte( )

, whose general form is shown here:

int ReadByte( )

Each time it is called, it reads a single byte from the file and returns it as an integer 

value. It returns –1 when the end of the file is encountered. Possible exceptions include 

NotSupportedException

 (the stream is not opened for input) and 



ObjectDisposedException

(the stream is closed).

To read a block of bytes, use 

Read( )

, which has this general form:

int Read(byte[ ] 

buf

, int 


offset

, int 


numBytes

)

Read( )

 attempts to read up to 

numBytes

 bytes into 



buf

 starting at 



buf

[

offset

]. It returns the 

number of bytes successfully read. An 



IOException

 is thrown if an I/O error occurs. Several 

other types of exceptions are possible, including 

NotSupportedException

, which is thrown 

if reading is not supported by the stream.

The following program uses 



ReadByte( )

 to input and display the contents of a text file, 

the name of which is specified as a command-line argument. Note the program handles two 

errors that might occur when this program is first executed: the specified file not being 

found or the user forgetting to include the name of the file.

/* Display a text file.

   To use this program, specify the name of the file that you

   want to see. For example, to see a file called TEST.CS,

   use the following command line.

   ShowFile TEST.CS

*/

using System;



using System.IO;

class ShowFile {

  static void Main(string[] args) {

www.freepdf-books.com




Download 4,07 Mb.

Do'stlaringiz bilan baham:
1   ...   482   483   484   485   486   487   488   489   ...   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