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
Input & output
SAIFUR RAHMAN
 
PPTX
Java I/O
Jayant Dalvi
 
PPT
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Don Bosco BSIT
 
PDF
Java IO
UTSAB NEUPANE
 
PDF
Java Day-6
People Strategists
 
PPTX
Files io
Narayana Swamy
 
PPTX
Input output files in java
Kavitha713564
 
PPT
Java basics
Jitender Jain
 
PPT
Md121 streams
Rakesh Madugula
 
PDF
Programming language JAVA Input output opearations
2025183005
 
PPT
Java development development Files lectur6.ppt
rafeakrafeak
 
PPT
Java Basics
shivamgarg_nitj
 
PPTX
Computer science input and output BASICS.pptx
RathanMB
 
PDF
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
PPTX
IOStream.pptx
HindAlmisbahi
 
PDF
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
PPS
Java session14
Niit Care
 
PPT
Using Input Output
raksharao
 
Input & output
SAIFUR RAHMAN
 
Java I/O
Jayant Dalvi
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Don Bosco BSIT
 
Java IO
UTSAB NEUPANE
 
Java Day-6
People Strategists
 
Files io
Narayana Swamy
 
Input output files in java
Kavitha713564
 
Java basics
Jitender Jain
 
Md121 streams
Rakesh Madugula
 
Programming language JAVA Input output opearations
2025183005
 
Java development development Files lectur6.ppt
rafeakrafeak
 
Java Basics
shivamgarg_nitj
 
Computer science input and output BASICS.pptx
RathanMB
 
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
IOStream.pptx
HindAlmisbahi
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
Java session14
Niit Care
 
Using Input Output
raksharao
 
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
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PDF
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
PPTX
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
DOCX
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
PDF
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
PDF
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
PDF
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
PDF
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
PPTX
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
PDF
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
PDF
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
PDF
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
PDF
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
PPTX
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
PDF
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
PPTX
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
DOCX
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
ICONIQ State of AI Report 2025 - The Builder's Playbook
Razin Mustafiz
 
Agentforce World Tour Toronto '25 - MCP with MuleSoft
Alexandra N. Martinez
 
Cryptography Quiz: test your knowledge of this important security concept.
Rajni Bhardwaj Grover
 
“NPU IP Hardware Shaped Through Software and Use-case Analysis,” a Presentati...
Edge AI and Vision Alliance
 
How do you fast track Agentic automation use cases discovery?
DianaGray10
 
SIZING YOUR AIR CONDITIONER---A PRACTICAL GUIDE.pdf
Muhammad Rizwan Akram
 
Transcript: Book industry state of the nation 2025 - Tech Forum 2025
BookNet Canada
 
AI Penetration Testing Essentials: A Cybersecurity Guide for 2025
defencerabbit Team
 
AI Agents in the Cloud: The Rise of Agentic Cloud Architecture
Lilly Gracia
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
“Squinting Vision Pipelines: Detecting and Correcting Errors in Vision Models...
Edge AI and Vision Alliance
 
Mastering Financial Management in Direct Selling
Epixel MLM Software
 
NLJUG Speaker academy 2025 - first session
Bert Jan Schrijver
 
CIFDAQ Market Wrap for the week of 4th July 2025
CIFDAQ
 
Agentforce World Tour Toronto '25 - Supercharge MuleSoft Development with Mod...
Alexandra N. Martinez
 
NASA A Researcher’s Guide to International Space Station : Physical Sciences ...
Dr. PANKAJ DHUSSA
 
From Sci-Fi to Reality: Exploring AI Evolution
Svetlana Meissner
 
Python coding for beginners !! Start now!#
Rajni Bhardwaj Grover
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 

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