Upload
mishallmujahid
View
218
Download
0
Embed Size (px)
Citation preview
8/9/2019 JAVA Streams n Filing
1/9
JAVA Software DevelopmentParadigm
Advance OO Programming
BSCS Semester 6MCS 3Course Instructor: Ms. Iram
8/9/2019 JAVA Streams n Filing
2/9
8/9/2019 JAVA Streams n Filing
3/9
14-3
Streams• A stream is deri,ed from se/uential in ut or out ut
de,ices.• An in ut stream roduces a stream of c!aracters0• An out ut stream recei,es a stream of c!aracters) $one
at a time.%• Streams a l( not +ust to files) 'ut also to I de,ices)Internet streams) and so on.
• A file can 'e treated as an in ut or out ut stream.•
In realit( file streams are 'uffered for efficienc(: it is notractical to read or rite one c!aracter at a time from
or to mass storage.• It is common to treat te"t files as streams.
8/9/2019 JAVA Streams n Filing
4/9
14-4
+a,a.ioBufferedIn utStreamBuffered ut utStreamBuffered eaderBuffered riterB(teArra(In utStreamB(teArra( ut utStreamC!arArra( eaderC!arArra( riter
ataIn utStreamata ut utStream
FileFile escri torFileIn utStreamFile ut utStreamFile5ermissionFile eaderFile riterFilterIn utStreamFilter ut utStreamFilter eaderFilter riter
In utStreamIn utStream eader
ine7um'erIn utStreamine7um'er eader'+ectIn utStream'+ectIn utStream.8etField'+ect ut utStream'+ect ut utStream.5utField'+ectStreamClass'+ectStreamFieldut utStreamut utStream riter
5i edIn utStream5i ed ut utStream5i ed eader5i ed riter5rintStream5rint riter5us!'ac9In utStream5us!'ac9 eader
andomAccessFileeader
Se/uenceIn utStreamSeriali a'le5ermissionStream o9eni erStringBufferIn utStreamString eaderString riter
riter
;o do I
read an int from a file
8/9/2019 JAVA Streams n Filing
5/9
14-=
+a,a.io #cont>d&• ?ses four !ierarc!ies of classes rooted at
eader ) riter ) In utStream ) ut utStream .• In utStream @ut utStream !ierarc!ies deal
it! '(tes. eader @riter !ierarc!ies dealit! c!ar s.
• ;as a s ecial stand-alone classandomAccessFile .
• !e Scanner class !as 'een added to +a,a.util to facilitate reading num'ers and
ords.
8/9/2019 JAVA Streams n Filing
6/9
14-6
+a,a.io.File• !e File class re resents a file #or folder& in
t!e file director( s(stem.
• Met!ods:String get7ame#&String getA'solute5at!#&long lengt!#&'oolean is irector(#&File listFiles#&
String at!name D..@ ata@ ords.t"t$0File file ne File# at!name&0
8/9/2019 JAVA Streams n Filing
7/9
eading Files using scanner
E-E
File file ne File#Dm(file.t"tD&0
Scanner in ut null0 tr( in ut ne Scanner#file&0 !ile#in ut.!as7e"t ine#&&
S(stem.out. rintln#in ut.ne"t ine#&&0 G catc! #File7otFound*"ce tion e"& S(stem.out. rintln#DHHH Cannot o en File&0 S(stem.e"it#&0 @@ /uit t!e rogram G
finall(in ut.close#&0
G
8/9/2019 JAVA Streams n Filing
8/914-
Scanner Met!ods
'oolean !as7e"t ine#&String ne"t ine#&'oolean !as7e"t#&String ne"t#&'oolean !as7e"tInt#&int ne"tInt#&'oolean !as7e"t ou'le#&dou'le ne"t ou'le#&,oid close#&
eads one
ord
8/9/2019 JAVA Streams n Filing
9/914-J
riting to a e"t File String at!name Dout ut.t"tD0 File file ne File# at!name&0 5rint riter out ut null0 try
out ut ne 5rint riter# file&0 G catch #File7otFound*"ce tion e"& S(stem.out. rintln#DCannot create D K at!name&0 S(stem.e"it#1&0 @@ /uit t!e rogram G out ut. rintln#$an( data%&0out ut.flus!#&0out ut.close#&0
e/uired to flus!t!e out ut 'uffer