You are on page 1of 3

I

nputandout
putst
ream i
njav
a-

Jav
aI/
O (
InputandOut
put
)i t
sused opr
ocesst
hei  
nputand 
producet
heout .
put

Javausestheconceptofast
ream t
omakeI/Ooper
ati
onf
ast
.Thej
ava.
iopackagecont
ainsal
lthe
cl
assesrequi
redfori
nputandout
putoper
ati
ons.

Wecanper
for
m f
il
ehandl
ingi
nJav
a byJav
aI/
OAPI
.

St
ream

Ast
ream i
sasequenceofdat
a.I
nJav
a,ast
ream i
scomposedofby
tes.

I
nJav
a,3st
reamsar
ecr
eat
edf
orusaut
omat
ical
l
y.Al
lthesest
reamsar
eat
tachedwi
tht
heconsol
e.

1)Sy
stem.
out

standar
dout
putst
ream

2)Sy
stem.
in:
 st
andar
dinputst
ream

3)Sy
stem.
err

standar
der
rorst
ream

Let
'sseet
hecodet
opr
int
 out
putandaner
ror
 messaget
otheconsol
e.

1. Sy
stem.
out.
pri
ntl
n("
simpl
e message")

 
2. Sy
stem.
err
.pr
int
ln(
"er
ror
 message")

 

Theexpl
anat
ionofOut
put
Str
eam andI
nput
Str
eam cl
assesar
egi
venbel
ow:

Out
put
Str
eam

Jav
aapplicat
ionusesanout putst
ream t
owr
it
edat
atoadest
inat
ion;
itmaybeaf
il
e,anar
ray
,
per
ipher
aldeviceorsocket
.

I
nput
Str
eam

Jav
aapplicat
ionusesani nputst
ream t
oreaddat
afr
om asour
ce;
itmaybeaf
il
e,anar
ray
,
per
ipher
aldeviceorsocket.

Let
'sunder
standt
hewor
kingofJav
aOut
put
Str
eam andI
nput
Str
eam byt
hef
igur
egi
venbel
owas-
Exampl
e-

1.
Readaf
il
eli
nebyl
ineusi
ngScannercl
ass

i
mpor
tjav
a.i
o.*
;
i
mpor
tjav
a.ut
il
.Scanner
;

publicclassReadLineBy LineExampl e2{


publicstati
cv oidmai n(St r
ingar gs[]){
tr
y{
//t
hef i
letobeopenedf orr eading
Fil
eInput Stream f is=new
Fil
eInputStr
eam( "C\\
Nay an\ \Deskt op\ \Demo. t
xt"
);
Scannersc=newScanner (
fi
s);//fi
let
obescanned
//r
eturnstrueifthereisanot herl i
net or ead
whi l
e( sc.hasNext Line( )
){
System. out.printl
n(sc.next
Line(
));
//r
etur
nsthel
i
net
hatwas
ski
pped
}
sc.cl
ose( );//cl osest hescanner
}catch( I
OExcept i
one){
e.pri
ntStackTr ace() ;
}
}
}

Example-Wr i
tet
hefi
leusi
ngFileWrit
ercl
assI
nthi
sexampl
e,wear
ewr
it
ingt
hedat
aint
hef
il
e
test
out
.txtusi
ngJav
aFileWri
tercl
ass.

packagecom. test
i
mpor tjav a.i
o.FileWriter;
publi
ccl assFi l
eWr i
terExampl e{
publicst ati
cv oidmai n(
St r
ingargs[]
){
try{
FileWr iterfw=newFi leWr i
ter
("C\\
Nay an\\Desktop\\
Demo.
txt
""
);
fw. wr i
te("Velocitycorporatetrai
ningcenterpune.
")
;
fw. close();
}catch(Except i
one) {System.out.
print
ln(
e);
}
Syst em. out.pri
ntln("
Success...
");
}
}

You might also like