SlideShare a Scribd company logo
www.webstackacademy.com
Java Programming Language SE – 6
Module 10: I/O Fundamentals
www.webstackacademy.com
Objectives
● Write a program that uses command-line arguments and system
properties
● Examine the Properties class
● Construct node and processing streams, and use them appropriately
● Serialize and deserialize objects
● Distinguish readers and writers from streams, and select
appropriately between them
www.webstackacademy.com
Command-Line Arguments
● Any Java technology application can use command-line arguments.
● These string arguments are placed on the command line to launch
the Java interpreter after the class name:
java TestArgs arg1 arg2 "another arg"
● Each command-line argument is placed in the args array that is
passed to the static main method:
public static void main(String[] args)
www.webstackacademy.com
Command-Line Arguments
public class TestArgs {
public static void main(String[] args) {
for ( int i = 0; i < args.length; i++ ) {
System.out.println("args[" + i + "] is ’" + args[i] + "’");
}}}
● Example execution:
java TestArgs arg0 arg1 "another arg"
args[0] is ’arg0’
args[1] is ’arg1’
args[2] is ’another arg’
www.webstackacademy.com
System Properties
● System properties are a feature that replaces the concept of
environment variables (which are platform-specific).
● The System.getProperties method returns a Properties object.
● The getProperty method returns a String representing the value of the
named property.
● Use the -D option on the command line to include a new property.
www.webstackacademy.com
The Properties Class
● The Properties class implements a mapping of names to values (a
String-to-String map).
● The propertyNames method returns an Enumeration of all property
names.
● The getProperty method returns a String representing the value of the
named property.
● You can also read and write a properties collection into a file using
load and store.
www.webstackacademy.com
The Properties Class
import java.util.Properties;
import java.util.Enumeration;
public class TestProperties {
public static void main(String[] args) {
Properties props = System.getProperties();
props.list(System.out);
}
}
www.webstackacademy.com
The Properties Class
The following is an example test run of this program:
java -DmyProp=theValue TestProperties
● The following is the (partial) output:
java.runtime.name=Java(TM) SE Runtime Environment
sun.boot.library.path=C:jsejdk1.6.0jrebin
java.vm.version=1.6.0-b105
java.vm.vendor=Sun Microsystems Inc.
java.vm.name=Java HotSpot(TM) Client VM
file.encoding.pkg=sun.io
user.country=US
myProp=theValue
Stream
www.webstackacademy.com
I/O Stream Fundamentals
● A stream is a flow of data from a source or to a sink.
● A source stream initiates the flow of data, also called an input stream.
● A sink stream terminates the flow of data, also called an output
stream.
● Sources and sinks are both node streams.
● Types of node streams are files, memory, and pipes between threads
or processes.
www.webstackacademy.com
Fundamental Stream
Classes
www.webstackacademy.com
Data Within Streams
● Java technology supports two types of streams: character and byte.
● Input and output of character data is handled by readers and writers.
● Input and output of byte data is handled by input streams and output
streams:
● Normally, the term stream refers to a byte stream.
● The terms reader and writer refer to character streams.
www.webstackacademy.com
The InputStream Methods
● The three basic read methods are:
– int read()
– int read(byte[] buffer)
– int read(byte[] buffer, int offset, int length)
● Other methods include:
– void close()
– int available()
– long skip(long n)
– boolean markSupported()
– void mark(int readlimit)
– void reset()
www.webstackacademy.com
The OutputStream Methods
● The three basic write methods are:
– void write(int c)
– void write(byte[] buffer)
– void write(byte[] buffer, int offset, int length)
● Other methods include:
– void close()
– void flush()
www.webstackacademy.com
The Reader Methods
● The three basic read methods are:
– int read()
– int read(char[] cbuf)
– int read(char[] cbuf, int offset, int length)
● Other methods include:
– void close()
– boolean ready()
– long skip(long n)
– boolean markSupported()
– void mark(int readAheadLimit)
– void reset()
www.webstackacademy.com
The Writer Methods
● The basic write methods are:
– voidwrite(int c)
– Void write(char[] cbuf)
– Void write(char[]cbuf, int offset, int length)
– Void write(String string)
– Void write(String string, int offset, int length)
● Other methods include:
– void close()
– void flush()
www.webstackacademy.com
Node Streams
www.webstackacademy.com
A Simple Example
This program performs a copy file operation using a manual
buffer: java TestNodeStreams file1 file2
public static void main(String[] args) {
try {
FileReader input = new FileReader(args[0]);
try {
FileWriter output = new FileWriter(args[1]);
try {
char[] buffer = new char[128];
int charsRead;
charsRead = input.read(buffer);
while ( charsRead != -1 ) {
// next slide
www.webstackacademy.com
A Simple Examples
output.write(buffer, 0, charsRead);
charsRead = input.read(buffer);
}
} finally {
output.close();}
} finally {
input.close();}
} catch (IOException e) {
e.printStackTrace();
}}
www.webstackacademy.com
Buffered Streams
This program performs a copy file operation using a built-in buffer: java TestBufferedStreams file1
file2
public static void main(String[] args) {
try {
FileReader input = new FileReader(args[0]);
BufferedReader bufInput = new BufferedReader(input);
try {
FileWriter output = new FileWriter(args[1]);
BufferedWriter bufOutput= new BufferedWriter(output);
try {
String line;
line = bufInput.readLine();
while ( line != null ) {
www.webstackacademy.com
Buffered Streams
bufOutput.write(line, 0, line.length());
bufOutput.newLine();
line = bufInput.readLine();
}
} finally {
bufOutput.close();
}
} finally {
bufInput.close();
}
} catch (IOException e) {
e.printStackTrace();
}}
www.webstackacademy.com
I/O Stream Chaining
www.webstackacademy.com
Processing Streams
www.webstackacademy.com
Processing Streams
www.webstackacademy.com
The InputStream
Class Hierarchy
www.webstackacademy.com
The OutputStream
Class Hierarchy
www.webstackacademy.com
The ObjectInputStream and
The ObjectOutputStream
Classes
● The Java API provides a standard mechanism (called object
serialization) that completely automates the process of writing and
reading objects from streams.
● When writing an object, the object input stream writes the class
name, followed by a description of the data members of the class, in
the order they appear in the stream, followed by the values for all the
fields on that object.
www.webstackacademy.com
The ObjectInputStream and
The ObjectOutputStream
Classes
● When reading an object, the object output stream reads the name of
the class and the description of the class to match against the class
in memory, and it reads the values from the stream to populate a
newly allocation instance of that class.
● Persistent storage of objects can be accomplished if files (or other
persistent storage) are used as streams.
www.webstackacademy.com
Input Chaining
Combinations: A Review
www.webstackacademy.com
Output Chaining
Combinations: A Review
www.webstackacademy.com
Serialization
● Serialization is a mechanism for saving the objects as a sequence of
bytes and rebuilding them later when needed.
● When an object is serialized, only the fields of the object are
preserved
● When a field references an object, the fields of the referenced object
are also serialized
● Some object classes are not serializable because their fields
represent transient operating system-specific information.
www.webstackacademy.com
The SerializeDate Class
public class SerializeDate implements Serializable{
SerializeDate() {
Date d = new Date ();
try {
FileOutputStream f =
new FileOutputStream ("date.ser");
ObjectOutputStream s =
new ObjectOutputStream (f);
s.writeObject (d);
s.close ();
} catch (IOException e) {
e.printStackTrace ();
}
www.webstackacademy.com
The SerializeDate Class
public static void main (String args[]) {
new SerializeDate();
}}
www.webstackacademy.com
The DeSerializeDate Class
public class DeSerializeDate {
DeSerializeDate () {
Date d = null;
try {
FileInputStream f =
new FileInputStream ("date.ser");
ObjectInputStream s =
new ObjectInputStream (f);
d = (Date) s.readObject ();
s.close ();
} catch (Exception e) {
e.printStackTrace ();
}
www.webstackacademy.com
The SerializeDate Class
public class SerializeDate implements Serializable{
SerializeDate() {
Date d = new Date ();
try {
FileOutputStream f =
new FileOutputStream ("date.ser");
ObjectOutputStream s =
new ObjectOutputStream (f);
s.writeObject (d);
s.close ();
} catch (IOException e) {
e.printStackTrace ();
}
www.webstackacademy.com
The DeSerializeDate Class
System.out.println(
"Deserialized Date object from date.ser");
System.out.println("Date: "+d);
}
public static void main (String args[]) {
new DeSerializeDate();
}}
www.webstackacademy.com
The Reader Class Hierarchy
www.webstackacademy.com
The Writer Class Hierarchy
www.webstackacademy.com
Web Stack Academy (P) Ltd
#83, Farah Towers,
1st floor,MG Road,
Bangalore – 560001
M: +91-80-4128 9576
T: +91-98862 69112
E: info@www.webstackacademy.com

More Related Content

What's hot (18)

PPTX
Pune-Cocoa: Blocks and GCD
Prashant Rane
 
PDF
Advanced Production Debugging
Takipi
 
ODP
Introduction to LAVA Workload Scheduler
Nopparat Nopkuat
 
PDF
Java collections the force awakens
RichardWarburton
 
PPTX
Java8.part2
Ivan Ivanov
 
PPTX
Serialization in java
Janu Jahnavi
 
ODP
Introduction to Java 8
Knoldus Inc.
 
PDF
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
ODP
Synapseindia reviews.odp.
Tarunsingh198
 
PDF
Streams in Java 8
Tobias Coetzee
 
PDF
Collections forceawakens
RichardWarburton
 
PPT
XML SAX PARSING
Eviatar Levy
 
PDF
TWJUG x Oracle Groundbreakers 2019 Taiwan - What’s New in Last Java Versions
Joseph Kuo
 
PPSX
Object Class
Hitesh-Java
 
PDF
JCConf 2018 - Retrospect and Prospect of Java
Joseph Kuo
 
PPTX
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Soumen Santra
 
PPT
Java user group 2015 02-09-java8
marctritschler
 
PDF
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 
Pune-Cocoa: Blocks and GCD
Prashant Rane
 
Advanced Production Debugging
Takipi
 
Introduction to LAVA Workload Scheduler
Nopparat Nopkuat
 
Java collections the force awakens
RichardWarburton
 
Java8.part2
Ivan Ivanov
 
Serialization in java
Janu Jahnavi
 
Introduction to Java 8
Knoldus Inc.
 
Java OOP Programming language (Part 8) - Java Database JDBC
OUM SAOKOSAL
 
Synapseindia reviews.odp.
Tarunsingh198
 
Streams in Java 8
Tobias Coetzee
 
Collections forceawakens
RichardWarburton
 
XML SAX PARSING
Eviatar Levy
 
TWJUG x Oracle Groundbreakers 2019 Taiwan - What’s New in Last Java Versions
Joseph Kuo
 
Object Class
Hitesh-Java
 
JCConf 2018 - Retrospect and Prospect of Java
Joseph Kuo
 
Java basic part 2 : Datatypes Keywords Features Components Security Exceptions
Soumen Santra
 
Java user group 2015 02-09-java8
marctritschler
 
Java OOP Programming language (Part 4) - Collection
OUM SAOKOSAL
 

Similar to Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals (20)

PPTX
java: basics, user input, data type, constructor
Shivam Singhal
 
PPTX
Java For beginners and CSIT and IT students
Partnered Health
 
PDF
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Matt Fuller
 
PPT
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
PDF
Core Java Programming Language (JSE) : Chapter XIII - JDBC
WebStackAcademy
 
PDF
Advance java kvr -satya
Satya Johnny
 
PDF
Adv kvr -satya
Jyothsna Sree
 
PDF
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
PPTX
Java programing language unit 1 introduction
chnrketan
 
PDF
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
WebStackAcademy
 
PPT
Annotations
Knoldus Inc.
 
PPT
Java findamentals1
Todor Kolev
 
PPT
Java findamentals1
Todor Kolev
 
PPT
Java findamentals1
Todor Kolev
 
PPT
Java Fundamentals.pptJava Fundamentals.ppt
yatakonakiran2
 
PDF
Java features. Java 8, 9, 10, 11
Ivelin Yanev
 
ODP
Slickdemo
Knoldus Inc.
 
PDF
An Introduction to Java Compiler and Runtime
Omar Bashir
 
PPTX
objectorientedprogrammingCHAPTER 2 (OOP).pptx
tutorialclassroomhit
 
PPTX
Unit No 5 Files and Database Connectivity.pptx
DrYogeshDeshmukh1
 
java: basics, user input, data type, constructor
Shivam Singhal
 
Java For beginners and CSIT and IT students
Partnered Health
 
Presto Testing Tools: Benchto & Tempto (Presto Boston Meetup 10062015)
Matt Fuller
 
Jacarashed-1746968053-300050282-Java.ppt
DilipDas70
 
Core Java Programming Language (JSE) : Chapter XIII - JDBC
WebStackAcademy
 
Advance java kvr -satya
Satya Johnny
 
Adv kvr -satya
Jyothsna Sree
 
Advanced java jee material by KV Rao sir
AVINASH KUMAR
 
Java programing language unit 1 introduction
chnrketan
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
WebStackAcademy
 
Annotations
Knoldus Inc.
 
Java findamentals1
Todor Kolev
 
Java findamentals1
Todor Kolev
 
Java findamentals1
Todor Kolev
 
Java Fundamentals.pptJava Fundamentals.ppt
yatakonakiran2
 
Java features. Java 8, 9, 10, 11
Ivelin Yanev
 
Slickdemo
Knoldus Inc.
 
An Introduction to Java Compiler and Runtime
Omar Bashir
 
objectorientedprogrammingCHAPTER 2 (OOP).pptx
tutorialclassroomhit
 
Unit No 5 Files and Database Connectivity.pptx
DrYogeshDeshmukh1
 
Ad

More from WebStackAcademy (20)

PDF
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
PDF
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
PDF
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
PDF
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
PDF
Webstack Academy - Internship Kick Off
WebStackAcademy
 
PDF
Building Your Online Portfolio
WebStackAcademy
 
PDF
Front-End Developer's Career Roadmap
WebStackAcademy
 
PDF
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
PDF
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
PDF
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
PDF
Angular - Chapter 5 - Directives
WebStackAcademy
 
PDF
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
PDF
Angular - Chapter 3 - Components
WebStackAcademy
 
PDF
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
PDF
Angular - Chapter 1 - Introduction
WebStackAcademy
 
PDF
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
PDF
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
PDF
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
PDF
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Webstack Academy - Course Demo Webinar and Placement Journey
WebStackAcademy
 
WSA: Scaling Web Service to Handle Millions of Requests per Second
WebStackAcademy
 
WSA: Course Demo Webinar - Full Stack Developer Course
WebStackAcademy
 
Career Building in AI - Technologies, Trends and Opportunities
WebStackAcademy
 
Webstack Academy - Internship Kick Off
WebStackAcademy
 
Building Your Online Portfolio
WebStackAcademy
 
Front-End Developer's Career Roadmap
WebStackAcademy
 
Angular - Chapter 9 - Authentication and Authorization
WebStackAcademy
 
Angular - Chapter 7 - HTTP Services
WebStackAcademy
 
Angular - Chapter 6 - Firebase Integration
WebStackAcademy
 
Angular - Chapter 5 - Directives
WebStackAcademy
 
Angular - Chapter 4 - Data and Event Handling
WebStackAcademy
 
Angular - Chapter 3 - Components
WebStackAcademy
 
Angular - Chapter 2 - TypeScript Programming
WebStackAcademy
 
Angular - Chapter 1 - Introduction
WebStackAcademy
 
JavaScript - Chapter 10 - Strings and Arrays
WebStackAcademy
 
JavaScript - Chapter 15 - Debugging Techniques
WebStackAcademy
 
JavaScript - Chapter 14 - Form Handling
WebStackAcademy
 
JavaScript - Chapter 13 - Browser Object Model(BOM)
WebStackAcademy
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Ad

Recently uploaded (20)

PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PPTX
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
PDF
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
PDF
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
PDF
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PPTX
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
PPTX
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
Blockchain Transactions Explained For Everyone
CIFDAQ
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
July Patch Tuesday
Ivanti
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
Top iOS App Development Company in the USA for Innovative Apps
SynapseIndia
 
LLMs.txt: Easily Control How AI Crawls Your Site
Keploy
 
Newgen Beyond Frankenstein_Build vs Buy_Digital_version.pdf
darshakparmar
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Using FME to Develop Self-Service CAD Applications for a Major UK Police Force
Safe Software
 
Chris Elwell Woburn, MA - Passionate About IT Innovation
Chris Elwell Woburn, MA
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
✨Unleashing Collaboration: Salesforce Channels & Community Power in Patna!✨
SanjeetMishra29
 
"Autonomy of LLM Agents: Current State and Future Prospects", Oles` Petriv
Fwdays
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
Blockchain Transactions Explained For Everyone
CIFDAQ
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
July Patch Tuesday
Ivanti
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 

Core Java Programming Language (JSE) : Chapter X - I/O Fundamentals