Java io stream operation related introduction.

SHOUBHIK1111 9 views 27 slides Aug 05, 2024
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

java io operation


Slide Content

© 2017 Wiprowipro.com confidential1Sensitivity: Internal & Restricted
I/O Streams-
Introduction

Sensitivity: Internal & Restricted
Agenda
Introduction to I/O Streams
Predefined I/O Streams2

© 2017 Wiprowipro.com confidential3Sensitivity: Internal & Restricted
Objectives
At the end of this module, you will be able to:
Understand I/O Streams and its categories
Understand about predefined I/O Streams

© 2017 Wiprowipro.com confidential4Sensitivity: Internal & Restricted
I/OStreams

Sensitivity: Internal & Restricted
•JavaprogramsperformI/Othroughstreams.Astreamis:
–anabstractionthateitherproducesorconsumesinformation
–linkedtoaphysicaldevicebytheJavaI/Osystem
•Allstreamsbehavesimilarly,eveniftheactualphysicaldevicestowhichtheyarelinked
differ.
•ThusthesameI/Oclassescanbeappliedtoanykindof
deviceastheyabstractthedifferencebetweendifferentI/Odevices.
I/OStreams

Sensitivity: Internal & Restricted
•Java’sstreamclassesaredefinedinthejava.iopackage.
•Java2definestwotypesofstreams:
–bytestreams
–characterstreams
•Bytestreams:
–provideaconvenientmeansforhandlinginputandoutputofbytes
–areusedforreadingorwritingbinarydata
•Characterstreams:
–provideaconvenientmeansforhandlinginputandoutputofcharacters
–useUnicode,and,therefore,canbeinternationalized
I/OStreams(Contd.).

Sensitivity: Internal & Restricted
•Systemclassofthejava.langpackagecontainsthreepredefinedstreamvariables,in,out
anderr.
•ThesevariablesaredeclaredaspublicandstaticwithinSystem:
–System.outreferstothestandardoutputstreamwhichistheconsole.
–System.inreferstostandardinput,whichisthekeyboardbydefault.
–System.errreferstothestandarderrorstream,whichalsoistheconsolebydefault.
ThePredefinedStreams

Sensitivity: Internal & Restricted
•System.outsendstheoutputtothestandardoutput stream,whichisconsolebydefault.
•System.err sends the output to the standard error stream, which also happens to be
console by default
•Thereasonbehindhavingtwoseparatestreamsforoutputanderroristhatthestandardo
utputshould beusedforregularprogramoutputswhilestandard
errorshouldbeusedforerrormessages.
DifferencebetweenSystem.outandSystem.err

Sensitivity: Internal & Restricted
•Boththesestreamscanberedirectedtodifferent destinations.
•Wecanredirecttheprogramoutputto a particularlogfileandtheerrormessagestoanother
logfilebyusingthefollowingsyntax:
javaStreamDemo>output.log2>error.log
(whereStreamDemoisthenameoftheclass)
Duringexecution,theprogramoutputwill be stored in output.log while the error message(if
any) will be stored in error.log
System.outandSystem.err(Contd.).

© 2017 Wiprowipro.com confidential10Sensitivity: Internal & Restricted
DemonstrationofSystem.outandSystem.err
classStreamDemo{
publicstaticvoidmain(String[]args){
try{
System.out.print("Writingprogramoutputtotheoutputfile");
inti=0;
intz=100/i;
}
catch(Exception e){
System.err.print("ArithmeticException hasoccured");
}
}
}

Sensitivity: Internal & Restricted
•SystemPropertiesprovideinformationaboutlocalsystemconfiguration.
•WhentheJavaVirtualMachinestarts,itinsertslocalSystemPropertiesintoaSystem
propertieslist.
•We can use methods definedin System classto
accessorchangethevaluesoftheseproperties.
publicstaticPropertiesgetProperties()
publicstaticStringgetProperty(Stringkey)
publicstaticvoidsetProperties(Propertiesprp)
SystemProperties

Sensitivity: Internal & Restricted
SomeoftheImportantPropertiesarelistedbelow:
SystemProperties(contd.).
Key DescriptionofAssociatedValue
java.version JavaRuntimeEnvironmentversion
java.home Javainstallationdirectory
java.class.pathJavaclasspath
os.name Operatingsystemname
user.name User'saccountname
user.home User'shomedirectory
user.dir User'scurrentworkingdirectory

Sensitivity: Internal & Restricted
publicstaticPropertiesgetProperties()
TheSystem.getProperties()methodreturnsan objectofthetypeProperties.
YoucanusethismethodtolistalltheSystem Properties.
System.getProperties()

© 2017 Wiprowipro.com confidential14Sensitivity: Internal & Restricted
DemoofSystem.getProperties()method
importjava.util.*;
classGetPropertiesDemo {
publicstaticvoidmain(String[]args){
Propertiesx=System.getProperties();
x.list(System.out);
}
}

Sensitivity: Internal & Restricted
publicstaticStringgetProperty(Stringkey)
YoucanalsouseSystem.getProperty(StringKey)methodtogetthevalueofaparticular
propertyrepresentedwithakey.
System.getProperty(StringKey)

Sensitivity: Internal & Restricted
classGetPropertyDemo {
publicstaticvoidmain(String[]args){
Stringuser_home=System.getProperty("user.home");
Stringjava_version=System.getProperty("java.version");
Stringjava_home=System.getProperty("java.home");
Stringclass_path=System.getProperty("java.class.path");
Stringos_name=System.getProperty("os.name");
Stringuser_name=System.getProperty("user.name");
Stringuser_dir=System.getProperty("user.dir");
System.out.println("Theuserhomedirectoryis"+user_home);
System.out.println("Thejavaversionis"+java_version);
System.out.println("TheJavaHomedirectoryis"+java_home);
System.out.println("Theclasspathissetto"+class_path);
System.out.println("TheOperatingSystemis"+os_name);
System.out.println("Theusernameis"+user_name);
System.out.println("Theworkingdirectoryis"+user_dir);
}
}
System.getProperty(StringKey)-Demo

© 2017 Wiprowipro.com confidential17Sensitivity: Internal & Restricted
System.getProperty(StringKey)-Demo
Output:
TheuserhomedirectoryisC:\Users\harb
Thejavaversionis1.6.0_05
TheJavaHomedirectoryisD:\ProgramFiles\java1.6\jdk1.6.0_05\jre
Theclasspathissetto.;E:\app\harb\product\11.1.0\db_1\oui\jlib\classes12.jar;E:\Training\PR
P2012JAVA\ExceltoJava\jxl-2.6.9.jar;
TheOperatingSystemisWindowsVista
Theusernameisharb
TheworkingdirectoryisD:\Java\day10\io

Sensitivity: Internal & Restricted
publicstaticvoidsetProperties(Propertiesprp)
TheSystem.setProperties(Propertiesprp)methodsetsthesystempropertiestotheProperti
es argument.
Youcanusethismethodtochangethesystem propertiesasperyourrequirement.
System.setProperties()

Sensitivity: Internal & Restricted
importjava.util.*;
publicclassSystemPropertiesDemo {
publicstaticvoidmain(String[]args){
System.out.print("Previous valueofJavaHome:");
System.out.println(System.getProperty("java.home"));
Propertiesp=System.getProperties();
p.put("java.home", "D:\\ProgramFiles\\java1.5\\jdk1.5.0_02");
System.setProperties(p);
System.out.print("New valueofJavaHome:");
System.out.println(System.getProperty("java.home"));
}
}
System.setProperties()-Demo
Theabovecodewhenexecuted,prints:
PreviousvalueofJavaHome:D:\ProgramFiles\java1.6\jdk1.6.0_05\jre
NewvalueofJavaHome:D:\ProgramFiles\java1.5\jdk1.5.0_02

Sensitivity: Internal & Restricted
ByteStreams
InputStream OutputStream
I/OStreamshierarchy
FileInputStre
am
ObjectInputStrea
m
BufferedInputStre
am
ManyMoreInputStreamclas
ses
ManyMoreOutputStreamclas
ses
FileOutputStrea
m
BufferedOutputStrea
m
ObjectOutputStre
am
Abstract
c lass
Abstract
class

Sensitivity: Internal & Restricted
CharacterStreams
Reader Writer
I/OStreamshierarchy(Contd.).
FileRead
er
InputStreamRea
der
BufferedRea
der
ManyMoreReaderclasses ManyMoreWriterclasses
FileWriter
BufferedWrit
er
OutputStreamWrit
er
Abstrac
tclass
Abstract
class

Sensitivity: Internal & Restricted
Toread&writedataintobuffer
ByteStreamclasses
Toread&writedataintofile
Toread&writeobjectinto
secondarydevice(serialization
)
FileInputStream
FIleOutputStream
BufferedInputStream
BufferedOutputStrea
m
ObjectInputStream
ObjectOutputStrea
m

Sensitivity: Internal & Restricted
To read & write data into buffer
Character Stream classes
To read & write data into file
Bridge from character stream
to byte stream
FileReader
FIleWriter
BufferedReader
BufferedWriter
InputStreamReade
r
OutputStreamWrit
er

Sensitivity: Internal & Restricted
•MatchthestreamswiththeappropriatephrasesincolumnB
ColumnA ColumnB
1.FileWriter Bytestreamforreadingfromfile
2.FileInputStream Characterstreamforreadingfromfile
3.FileOutputStream Characterstreamforwritingtoafile
4.FileReader Bytestreamforwritingtoafile
Matchthefollowing

Sensitivity: Internal & Restricted
1.Java input output classes are available in ___________package
a.java.lang
b.java.io
c.java.util
2.What are the inbuilt streams available in java.io package
a.System.in
b.System.out
c.System.err
d.All of the above
3.Can data flow through a given stream in both directions?
a.Yes
b.No
Quiz

© 2017 Wiprowipro.com confidential26Sensitivity: Internal & Restricted
Summary
Introduction to I/O Streams
Predefined I/O Streams

© 2017 Wiprowipro.com confidential27Sensitivity: Internal & Restricted © 2017 Wiprowipro.com confidential27
Thank You
Tags