SlideShare a Scribd company logo
I/O Streams
Visit http://gsbprogramming.blogspot.in
In computer science, a stream is a sequence of data elements made available over time. A stream can be thought of
as items on a conveyor belt being processed one at a time rather than in large batches.
Streams are processed differently from batch data :
1. Normal functions cannot operate on streams as a whole, as they have potentially unlimited data, and
formally,
2. Streams are codata (potentially unlimited), not data (which is finite).
STREAMS
An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of
sources and destinations, including disk files, devices, other programs, and memory arrays.
Streams support many different kinds of data, including simple bytes, primitive data types, localized characters,
and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways.
No matter how they work internally, all streams present the same simple model to programs that use them: A
stream is a sequence of data.
A program uses an input stream to read data from a source, one item at a time
A program uses an output stream to write data to a destination, one item at a time
I/O STREAMS
1. FileInputStream
2. DataInputStream
3. ObjectInputStream
4. SequenceInputStream
1. FileOutputStream
2. DataOutputStream
3. ObjectOutputStream
Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended
from InputStream and OutputStream.
1. FileReader
2. Buffered Reader
3. LineNumberReader
1. FileWriter
2. PrintWriter
A character stream will read a file character by character.The character streams are capable to read 16-bit characters
(byte streams read 8-bit characters). Character streams are capable to translate implicitly 8-bit data to 16-bit data or
vice versa. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc.But byte
stream is suitable only forASCII character set.
import java.io.*;
class A {
public static void main(String[] args) throws IOException {
FileInputStream fis = null;
int c;
try {
fis = new FileInputStream("D:BCD.txt");
while ((c = fis.read()) != -1) {
System.out.println("Value: " + c + " Character: " + (char) c);
}
}
finally{
if(fis !=null)
fis.close();
}
}
}
FileInputStream
Output:
Value: 65 Character: A
Value: 66 Character: B
Value: 67 Character: C
import java.io.*;
class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("D:BCD.txt");
out = new FileOutputStream("D:XYZ.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null)
in.close();
if (out != null)
out.close();
}
}
}
FileOutputStream
//Write PrimitiveTypes to File
DataOutputStream dos = null;
try {
dos= new DataOutputStream(new FileOutputStream("D:Data.txt"));
dos.writeInt(10); dos.writeDouble(20.3);
dos.writeBoolean(true);
}
finally{
if(dos !=null) dos.close();
}
//Read PrimitiveTypes From File
DataInputStream dis = null;
try {
dis = new DataInputStream(new FileInputStream("D:Data.txt"));
System.out.println("Int: "+dis.readInt());
System.out.println("Double: "+dis.readDouble());
System.out.println("Boolean: "+dis.readBoolean());
}
finally{
if(dis !=null)
dis.close();
}
DataInputStream & DataOutputStream
Output:
Int: 10
Double: 20.3
Boolean: true
Serialization in Java
• Java provides a mechanism, called object serialization where an object can be represented as
a sequence of bytes that includes the object's data as well as information about the object's
type and the types of data stored in the object.
• After a serialized object has been written into a file, it can be read from the file and de-
serialized that is, the type information and bytes that represent the object and its data can
be used to recreate the object in memory.
• Serialization is a mechanism of converting the state of an object into a byte stream. De-
serialization is the reverse process where the byte stream is used to recreate the actual Java
object in memory.This mechanism is used to persist the object.
The byte stream created is platform independent. So, the object serialized on one platform can be
deserialized on a different platform
A Java object is serializable if its class or any of its super classes implements either
the java.io.Serializable interface or its sub interface, java.io.Externalizable.
Deserialization is the process of converting the serialized form of an object back into a copy of the object.
Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that
objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and
Remote.
Points to remember
1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but
vice-versa is not true.
2. Only non-static data members are saved via Serialization process.
3. Static data members and transient data members are not saved via Serialization process.So, if you don’t
want to save value of a non-static data member then make it transient.
4. Constructor of object is never called when an object is deserialized.
public class Employee implements java.io.Serializable
{
public String name;
public String address;
public transient int SSN;
public int number;
public void mailCheck()
{
System.out.println("Mailing a check to " + name + " " + address);
}
}
import java.io.*;
public class SerializeDemo {
public static void main(String [] args) {
Employee e = new Employee();
e.name = "Reyan Ali";
e.address = "Phokka Kuan,Ambehta Peer";
e.SSN = 11122333;
e.number = 101;
try {
FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out.printf("Serialized data is saved in /tmp/employee.ser");
}catch(IOException i) {
i.printStackTrace();
}
}
}
Serialization in Java
import java.io.*;
public class DeserializeDemo {
public static void main(String [] args) {
Employee e = null;
try {
FileInputStream fileIn = new FileInputStream("/tmp/employee.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Employee) in.readObject();
in.close();
fileIn.close();
}catch(IOException i) {
i.printStackTrace();
return;
}catch(ClassNotFoundException c) {
System.out.println("Employee class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Employee...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("SSN: " + e.SSN);
System.out.println("Number: " + e.number);
}
}
De-Serialization in Java
Output
Deserialized Employee...
Name: Reyan Ali
Address:Phokka Kuan, Ambehta Peer
SSN: 0
Number:101
Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper
classes.
For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing.
Here is the simplest example of autoboxing:
Character ch = 'a';
The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing. Let's see the example of unboxing:
Integer i=new Integer(50);
int a=i;
Boxing and Un-Boxing in Java
//Used For Reading from multiple streams
FileInputStream input1 = new FileInputStream("D:A.txt");
FileInputStream input2 = new FileInputStream("D:B.txt");
SequenceInputStream inst = new SequenceInputStream(input1, input2);
int j;
while ((j = inst.read()) != -1) {
System.out.print((char) j);
}
inst.close();
input1.close();
input2.close();
SequenceInputStream
Output:
ABCXYZ
FileReader
import java.io.*;
class A {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("D:A.txt");
int r;
while((r=fr.read())!=-1)
System.out.print((char)r);
fr.close();
}
}
Output:
ABC
BufferedReader
import java.io.*;
class A {
public static void main(String[] args) throws IOException {
FileReader fr=new FileReader("D:A.txt");
BufferedReader br= new BufferedReader(fw);
String s;
while ((s =br.readLine() !=null){
System.out.println(s);
}
fr.close();
br.close();
}
}
FileWriter
import java.io.*;
class A {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("D:A.txt");
int r=65; //Character "A"
fw.write(r);
fw.close();
}
}
PrintWriter
import java.io.*;
class A {
public static void main(String[] args) throws IOException {
FileWriter fw=new FileWriter("D:A.txt");
PrintWriter pw=new PrintWriter(fw);
pw.println(“Hello”);
pw.close();
fw.close();
}
}
LineNumberReader
import java.io.*;
class A {
public static void main(String[] args) throws IOException {
LineNumberReader lnr=new LineNumberReader(new
FileReader("D:A.txt"));
System.out.println(lnr.getLineNumber());
System.out.println(lnr.readLine());
System.out.println(lnr.getLineNumber());
lnr.close();
}
Output:
0
Line1
1

More Related Content

What's hot

Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
Hiranya Jayathilaka
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
Anton Keks
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
Om Ganesh
 
Java I/O and Object Serialization
Java I/O and Object SerializationJava I/O and Object Serialization
Java I/O and Object Serialization
Navneet Prakash
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
babak danyal
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12Vince Vo
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsDon Bosco BSIT
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
Marcello Thiry
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in JavaCIB Egypt
 
Java file
Java fileJava file
Java file
sonnetdp
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQL
Raji Ghawi
 
Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
WebStackAcademy
 

What's hot (20)

Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Java Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and StreamsJava Course 8: I/O, Files and Streams
Java Course 8: I/O, Files and Streams
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Java I/O and Object Serialization
Java I/O and Object SerializationJava I/O and Object Serialization
Java I/O and Object Serialization
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
Jedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io StreamsJedi Slides Intro2 Chapter12 Advanced Io Streams
Jedi Slides Intro2 Chapter12 Advanced Io Streams
 
32.java input-output
32.java input-output32.java input-output
32.java input-output
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
 
Java file
Java fileJava file
Java file
 
Java and SPARQL
Java and SPARQLJava and SPARQL
Java and SPARQL
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/OCore Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
Core Java Programming Language (JSE) : Chapter XI - Console I/O and File I/O
 

Similar to IO Streams, Serialization, de-serialization, autoboxing

Gulshan serialization inJava PPT ex.pptx
Gulshan serialization inJava PPT ex.pptxGulshan serialization inJava PPT ex.pptx
Gulshan serialization inJava PPT ex.pptx
PRABHATMISHRA969924
 
Serialization in java
Serialization in javaSerialization in java
Serialization in java
Janu Jahnavi
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
PragatiSutar4
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
kanchanmahajan23
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
kanchanmahajan23
 
Basic of Javaio
Basic of JavaioBasic of Javaio
Basic of Javaio
suraj pandey
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
caswenson
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Outputphanleson
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
Java Programming - 06 java file io
Java Programming - 06 java file ioJava Programming - 06 java file io
Java Programming - 06 java file io
Danairat Thanabodithammachari
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptx1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptx
YashrajMohrir
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
AKR Education
 

Similar to IO Streams, Serialization, de-serialization, autoboxing (20)

Gulshan serialization inJava PPT ex.pptx
Gulshan serialization inJava PPT ex.pptxGulshan serialization inJava PPT ex.pptx
Gulshan serialization inJava PPT ex.pptx
 
Serialization in java
Serialization in javaSerialization in java
Serialization in java
 
File Handling.pptx
File Handling.pptxFile Handling.pptx
File Handling.pptx
 
Files io
Files ioFiles io
Files io
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
Basic of Javaio
Basic of JavaioBasic of Javaio
Basic of Javaio
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
 
Itp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & OutputItp 120 Chapt 19 2009 Binary Input & Output
Itp 120 Chapt 19 2009 Binary Input & Output
 
Java I/O
Java I/OJava I/O
Java I/O
 
Java Programming - 06 java file io
Java Programming - 06 java file ioJava Programming - 06 java file io
Java Programming - 06 java file io
 
Io stream
Io streamIo stream
Io stream
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptx1.26 File Input-Output in JAVA.pptx
1.26 File Input-Output in JAVA.pptx
 
Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction Unit 1 of java part 2 basic introduction
Unit 1 of java part 2 basic introduction
 
Java
JavaJava
Java
 

More from Gurpreet singh

Creating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP ReportsCreating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP Reports
Gurpreet singh
 
Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP Reporting
Gurpreet singh
 
Why Messaging system?
Why Messaging system?Why Messaging system?
Why Messaging system?
Gurpreet singh
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Gurpreet singh
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr Framework
Gurpreet singh
 
Java Servlet part 3
Java Servlet part 3Java Servlet part 3
Java Servlet part 3
Gurpreet singh
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuing
Gurpreet singh
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
Gurpreet singh
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
Gurpreet singh
 
Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
Gurpreet singh
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
Gurpreet singh
 
Java Servlets Part 2
Java Servlets Part 2Java Servlets Part 2
Java Servlets Part 2
Gurpreet singh
 
Creating business group in oracle apps
Creating business group in oracle appsCreating business group in oracle apps
Creating business group in oracle apps
Gurpreet singh
 
Defing locations in Oracle Apps
Defing locations in Oracle AppsDefing locations in Oracle Apps
Defing locations in Oracle Apps
Gurpreet singh
 
Assigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTAssigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYST
Gurpreet singh
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
Gurpreet singh
 
PL/SQL Part 3
PL/SQL Part 3PL/SQL Part 3
PL/SQL Part 3
Gurpreet singh
 
PL/SQL Part 2
PL/SQL Part 2PL/SQL Part 2
PL/SQL Part 2
Gurpreet singh
 
PL/SQL Part 1
PL/SQL Part 1PL/SQL Part 1
PL/SQL Part 1
Gurpreet singh
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)
Gurpreet singh
 

More from Gurpreet singh (20)

Creating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP ReportsCreating ESS Jobs for Oracle Fusion BIP Reports
Creating ESS Jobs for Oracle Fusion BIP Reports
 
Introduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP ReportingIntroduction to Oracle Fusion BIP Reporting
Introduction to Oracle Fusion BIP Reporting
 
Why Messaging system?
Why Messaging system?Why Messaging system?
Why Messaging system?
 
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...Understanding Flex Fields with  Accounting Flexfields(Chart of Accounts) in O...
Understanding Flex Fields with Accounting Flexfields(Chart of Accounts) in O...
 
Oracle Application Developmenr Framework
Oracle Application Developmenr FrameworkOracle Application Developmenr Framework
Oracle Application Developmenr Framework
 
Java Servlet part 3
Java Servlet part 3Java Servlet part 3
Java Servlet part 3
 
Oracle advanced queuing
Oracle advanced queuingOracle advanced queuing
Oracle advanced queuing
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
Oracle SQL Part 2
Oracle SQL Part 2Oracle SQL Part 2
Oracle SQL Part 2
 
Oracle SQL Part1
Oracle SQL Part1Oracle SQL Part1
Oracle SQL Part1
 
Generics and collections in Java
Generics and collections in JavaGenerics and collections in Java
Generics and collections in Java
 
Java Servlets Part 2
Java Servlets Part 2Java Servlets Part 2
Java Servlets Part 2
 
Creating business group in oracle apps
Creating business group in oracle appsCreating business group in oracle apps
Creating business group in oracle apps
 
Defing locations in Oracle Apps
Defing locations in Oracle AppsDefing locations in Oracle Apps
Defing locations in Oracle Apps
 
Assigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYSTAssigning role AME_BUS_ANALYST
Assigning role AME_BUS_ANALYST
 
PL/SQL Part 5
PL/SQL Part 5PL/SQL Part 5
PL/SQL Part 5
 
PL/SQL Part 3
PL/SQL Part 3PL/SQL Part 3
PL/SQL Part 3
 
PL/SQL Part 2
PL/SQL Part 2PL/SQL Part 2
PL/SQL Part 2
 
PL/SQL Part 1
PL/SQL Part 1PL/SQL Part 1
PL/SQL Part 1
 
Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)Introduction to Data Flow Diagram (DFD)
Introduction to Data Flow Diagram (DFD)
 

Recently uploaded

Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
Kumud Singh
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
mikeeftimakis1
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Aggregage
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
sonjaschweigert1
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
DianaGray10
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
Aftab Hussain
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
Alpen-Adria-Universität
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
DianaGray10
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 

Recently uploaded (20)

Mind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AIMind map of terminologies used in context of Generative AI
Mind map of terminologies used in context of Generative AI
 
Introduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - CybersecurityIntroduction to CHERI technology - Cybersecurity
Introduction to CHERI technology - Cybersecurity
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 
Generative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to ProductionGenerative AI Deep Dive: Advancing from Proof of Concept to Production
Generative AI Deep Dive: Advancing from Proof of Concept to Production
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...A tale of scale & speed: How the US Navy is enabling software delivery from l...
A tale of scale & speed: How the US Navy is enabling software delivery from l...
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5UiPath Test Automation using UiPath Test Suite series, part 5
UiPath Test Automation using UiPath Test Suite series, part 5
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Removing Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software FuzzingRemoving Uninteresting Bytes in Software Fuzzing
Removing Uninteresting Bytes in Software Fuzzing
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Video Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the FutureVideo Streaming: Then, Now, and in the Future
Video Streaming: Then, Now, and in the Future
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6UiPath Test Automation using UiPath Test Suite series, part 6
UiPath Test Automation using UiPath Test Suite series, part 6
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 

IO Streams, Serialization, de-serialization, autoboxing

  • 2. In computer science, a stream is a sequence of data elements made available over time. A stream can be thought of as items on a conveyor belt being processed one at a time rather than in large batches. Streams are processed differently from batch data : 1. Normal functions cannot operate on streams as a whole, as they have potentially unlimited data, and formally, 2. Streams are codata (potentially unlimited), not data (which is finite). STREAMS
  • 3. An I/O Stream represents an input source or an output destination. A stream can represent many different kinds of sources and destinations, including disk files, devices, other programs, and memory arrays. Streams support many different kinds of data, including simple bytes, primitive data types, localized characters, and objects. Some streams simply pass on data; others manipulate and transform the data in useful ways. No matter how they work internally, all streams present the same simple model to programs that use them: A stream is a sequence of data. A program uses an input stream to read data from a source, one item at a time A program uses an output stream to write data to a destination, one item at a time I/O STREAMS
  • 4. 1. FileInputStream 2. DataInputStream 3. ObjectInputStream 4. SequenceInputStream 1. FileOutputStream 2. DataOutputStream 3. ObjectOutputStream Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.
  • 5. 1. FileReader 2. Buffered Reader 3. LineNumberReader 1. FileWriter 2. PrintWriter A character stream will read a file character by character.The character streams are capable to read 16-bit characters (byte streams read 8-bit characters). Character streams are capable to translate implicitly 8-bit data to 16-bit data or vice versa. Character stream can support all types of character sets ASCII, Unicode, UTF-8, UTF-16 etc.But byte stream is suitable only forASCII character set.
  • 6. import java.io.*; class A { public static void main(String[] args) throws IOException { FileInputStream fis = null; int c; try { fis = new FileInputStream("D:BCD.txt"); while ((c = fis.read()) != -1) { System.out.println("Value: " + c + " Character: " + (char) c); } } finally{ if(fis !=null) fis.close(); } } } FileInputStream Output: Value: 65 Character: A Value: 66 Character: B Value: 67 Character: C
  • 7. import java.io.*; class CopyBytes { public static void main(String[] args) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream("D:BCD.txt"); out = new FileOutputStream("D:XYZ.txt"); int c; while ((c = in.read()) != -1) { out.write(c); } } finally { if (in != null) in.close(); if (out != null) out.close(); } } } FileOutputStream
  • 8. //Write PrimitiveTypes to File DataOutputStream dos = null; try { dos= new DataOutputStream(new FileOutputStream("D:Data.txt")); dos.writeInt(10); dos.writeDouble(20.3); dos.writeBoolean(true); } finally{ if(dos !=null) dos.close(); } //Read PrimitiveTypes From File DataInputStream dis = null; try { dis = new DataInputStream(new FileInputStream("D:Data.txt")); System.out.println("Int: "+dis.readInt()); System.out.println("Double: "+dis.readDouble()); System.out.println("Boolean: "+dis.readBoolean()); } finally{ if(dis !=null) dis.close(); } DataInputStream & DataOutputStream Output: Int: 10 Double: 20.3 Boolean: true
  • 9. Serialization in Java • Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. • After a serialized object has been written into a file, it can be read from the file and de- serialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory. • Serialization is a mechanism of converting the state of an object into a byte stream. De- serialization is the reverse process where the byte stream is used to recreate the actual Java object in memory.This mechanism is used to persist the object.
  • 10. The byte stream created is platform independent. So, the object serialized on one platform can be deserialized on a different platform
  • 11. A Java object is serializable if its class or any of its super classes implements either the java.io.Serializable interface or its sub interface, java.io.Externalizable. Deserialization is the process of converting the serialized form of an object back into a copy of the object. Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and Remote. Points to remember 1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-versa is not true. 2. Only non-static data members are saved via Serialization process. 3. Static data members and transient data members are not saved via Serialization process.So, if you don’t want to save value of a non-static data member then make it transient. 4. Constructor of object is never called when an object is deserialized.
  • 12. public class Employee implements java.io.Serializable { public String name; public String address; public transient int SSN; public int number; public void mailCheck() { System.out.println("Mailing a check to " + name + " " + address); } }
  • 13. import java.io.*; public class SerializeDemo { public static void main(String [] args) { Employee e = new Employee(); e.name = "Reyan Ali"; e.address = "Phokka Kuan,Ambehta Peer"; e.SSN = 11122333; e.number = 101; try { FileOutputStream fileOut = new FileOutputStream("/tmp/employee.ser"); ObjectOutputStream out = new ObjectOutputStream(fileOut); out.writeObject(e); out.close(); fileOut.close(); System.out.printf("Serialized data is saved in /tmp/employee.ser"); }catch(IOException i) { i.printStackTrace(); } } } Serialization in Java
  • 14. import java.io.*; public class DeserializeDemo { public static void main(String [] args) { Employee e = null; try { FileInputStream fileIn = new FileInputStream("/tmp/employee.ser"); ObjectInputStream in = new ObjectInputStream(fileIn); e = (Employee) in.readObject(); in.close(); fileIn.close(); }catch(IOException i) { i.printStackTrace(); return; }catch(ClassNotFoundException c) { System.out.println("Employee class not found"); c.printStackTrace(); return; } System.out.println("Deserialized Employee..."); System.out.println("Name: " + e.name); System.out.println("Address: " + e.address); System.out.println("SSN: " + e.SSN); System.out.println("Number: " + e.number); } } De-Serialization in Java Output Deserialized Employee... Name: Reyan Ali Address:Phokka Kuan, Ambehta Peer SSN: 0 Number:101
  • 15. Autoboxing is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper classes. For example, converting an int to an Integer, a double to a Double, and so on. If the conversion goes the other way, this is called unboxing. Here is the simplest example of autoboxing: Character ch = 'a'; The automatic conversion of wrapper class type into corresponding primitive type, is known as Unboxing. Let's see the example of unboxing: Integer i=new Integer(50); int a=i; Boxing and Un-Boxing in Java
  • 16. //Used For Reading from multiple streams FileInputStream input1 = new FileInputStream("D:A.txt"); FileInputStream input2 = new FileInputStream("D:B.txt"); SequenceInputStream inst = new SequenceInputStream(input1, input2); int j; while ((j = inst.read()) != -1) { System.out.print((char) j); } inst.close(); input1.close(); input2.close(); SequenceInputStream Output: ABCXYZ
  • 17. FileReader import java.io.*; class A { public static void main(String[] args) throws IOException { FileReader fr=new FileReader("D:A.txt"); int r; while((r=fr.read())!=-1) System.out.print((char)r); fr.close(); } } Output: ABC
  • 18. BufferedReader import java.io.*; class A { public static void main(String[] args) throws IOException { FileReader fr=new FileReader("D:A.txt"); BufferedReader br= new BufferedReader(fw); String s; while ((s =br.readLine() !=null){ System.out.println(s); } fr.close(); br.close(); } }
  • 19. FileWriter import java.io.*; class A { public static void main(String[] args) throws IOException { FileWriter fw=new FileWriter("D:A.txt"); int r=65; //Character "A" fw.write(r); fw.close(); } }
  • 20. PrintWriter import java.io.*; class A { public static void main(String[] args) throws IOException { FileWriter fw=new FileWriter("D:A.txt"); PrintWriter pw=new PrintWriter(fw); pw.println(“Hello”); pw.close(); fw.close(); } }
  • 21. LineNumberReader import java.io.*; class A { public static void main(String[] args) throws IOException { LineNumberReader lnr=new LineNumberReader(new FileReader("D:A.txt")); System.out.println(lnr.getLineNumber()); System.out.println(lnr.readLine()); System.out.println(lnr.getLineNumber()); lnr.close(); } Output: 0 Line1 1