SlideShare a Scribd company logo
12 Advanced I/O Streams
Topics General Stream Types Character and Byte Streams Input and Output Streams Node and Filter Streams The  File  Class Reader  Classes Reader  Methods Node  Reader  Classes Filter  Reader  Classes
Topics Writer  Classes Writer  Methods Node  Writer  Classes Filter  Writer  Classes InputStream  Classes InputStream  Methods Node  InputStream  Classes Filter  InputStream  Classes
Topics OutputStream  Classes OutputStream  Methods Node  OutputStream  Classes Filter  OutputStream  Classes Serialization The  transient  Keyword Serialization: Writing an Object Stream Deserialization: Reading an Object Stream
General Stream Types Streams Abstraction of a file or a device that allows a series of items to be read or written General Stream Categories Character and Byte Streams Input and Output Streams Node and Filter Streams
Character and Byte Streams Character streams File or device abstractions for Unicode characters Superclass of all classes for character streams: The  Reader  class The  Writer  class Both classes are  abstract Byte streams For binary data Root classes for byte streams: The  InputStream  Class The  OutputStream  Class Both classes are  abstract
Input and Output Streams Input or source streams Can read from these streams Superclasses of all input streams: The  InputStream  Class The  Reader  Class Output or sink streams Can write to these streams Root classes of all output streams: The  OutputStream  Class  The  Writer  Class
Node and Filter Streams Node streams Contain the basic functionality of reading or writing from a specific location Types of node streams include files, memory and pipes Filter streams Layered onto node streams between threads or processes For additional functionalities Adding layers to a node stream is called stream chaining
The  File  Class Not a stream class Important since stream classes manipulate  File  objects Abstract representation of actual files and directory pathnames
The  File  Class: Constructors Has four constructors
The  File  Class: Methods
The  File  Class: Methods
The  File  Class: Example import java.io.*; public class FileInfoClass { public static void main(String args[]) { String fileName = args[0]; File fn = new File(fileName); System.out.println("Name: " + fn.getName()); if (!fn.exists()) { System.out.println(fileName  + " does not exists."); //continued...
The  File  Class: Example /* Create a temporary directory instead. */ System.out.println("Creating temp  directory..."); fileName = "temp"; fn = new File(fileName); fn.mkdir(); System.out.println(fileName + (fn.exists()? "exists": "does not exist")); System.out.println("Deleting temp directory..."); fn.delete(); //continued...
The  File  Class: Example System.out.println(fileName + (fn.exists()? "exists": "does not exist")); return; } //end of: if (!fn.exists()) System.out.println(fileName + " is a " + (fn.isFile()? "file." :"directory.")); if (fn.isDirectory()) { String content[] = fn.list(); System.out.println("The content of this  directory:"); //continued...
The  File  Class: Example for (int i = 0; i < content.length; i++) { System.out.println(content[i]); } } //end of: if (fn.isDirectory()) if (!fn.canRead()) { System.out.println(fileName  + &quot; is not readable.&quot;); return; } //continued...
The  File  Class: Example System.out.println(fileName + &quot; is &quot; + fn.length() + &quot; bytes long.&quot;); System.out.println(fileName + &quot; is &quot; + fn.lastModified() + &quot; bytes long.&quot;); if (!fn.canWrite()) { System.out.println(fileName  + &quot; is not writable.&quot;); } } }
The  Reader  Class: Methods
The  Reader  Class: Methods
Node  Reader  Classes
Filter  Reader  Classes
The  Writer  Class: Methods
Node  Writer  Classes
Filter  Writer  Classes
Basic  Reader / Writer  Example import java.io.*; class CopyFile { void copy(String input, String output) { FileReader reader; FileWriter writer; int data; try { reader = new FileReader(input); writer = new FileWriter(output); //continued...
Basic  Reader / Writer  Example while ((data =  reader.read() ) != -1) { writer.write(data); } reader.close(); writer.close(); } catch (IOException ie) { ie.printStackTrace(); } } //continued...
Basic  Reader / Writer  Example public static void main(String args[]) { String inputFile = args[0]; String outputFile = args[1]; CopyFile cf = new CopyFile(); cf.copy(inputFile, outputFile); } }
Modified  Reader / Writer  Example import java.io.*; class CopyFile { void copy(String input, String output) { BufferedReader reader; BufferedWriter writer; String data; try { reader = new  BufferedReader(new FileReader(input)); writer = new  BufferedWriter(new FileWriter(output)); //continued...
Modified  Reader / Writer  Example while ((data =  reader.readLine() ) != null) { writer.write(data, 0, data.length); } reader.close(); writer.close(); } catch (IOException ie) { ie.printStackTrace(); } } //continued...
Modified  Reader / Writer  Example public static void main(String args[]) { String inputFile = args[0]; String outputFile = args[1]; CopyFile cf = new CopyFile(); cf.copy(inputFile, outputFile); } }
The  InputStream  Class: Methods
The  InputStream  Class: Methods
Node  InputStream  Classes
Filter  InputStream  Classes
The  OutputStream  Class: Methods
Node  OutputStream  Classes
Filter  OutputStream  Classes
Basic  InputStream /  OutputStream  Example import java.io.*; class CopyFile { void copy(String input, String output) { FileInputStream inputStr; FileOutputStream outputStr; int data; try { inputStr = new FileInputStream(input); outputStr = new FileOutputStream(output); //continued...
Basic  InputStream /  OutputStream  Example while ((data =  inputStr.read() ) != -1) { outputStr.write(data); } inputStr.close(); outputStr.close(); } catch (IOException ie) { ie.printStackTrace(); } } //continued...
Basic  InputStream /  OutputStream  Example public static void main(String args[]) { String inputFile = args[0]; String outputFile = args[1]; CopyFile cf = new CopyFile(); cf.copy(inputFile, outputFile); } }
Modified  InputStream /  OutputStream  Example import java.io.*; class CopyFile { void copy(String input) { PushbackInputStream inputStr; PrintStream outputStr; int data; try { inputStr = new PushbackInputStream(new  FileInputStream(input)); outputStr = new PrintStream(System.out); //continued...
Modified  InputStream /  OutputStream  Example while ((data =  inputStr.read() ) != -1) { outputStr.println(&quot;read data: &quot; +  (char) data); inputStr.unread(data); data = inputStr.read(); outputStr.println(&quot;unread data: &quot; + (char) data); } inputStr.close(); outputStr.close(); //continued...
Modified  InputStream /  OutputStream  Example } catch (IOException ie) { ie.printStackTrace(); } } public static void main(String args[]) { String inputFile = args[0]; CopyFile cf = new CopyFile(); cf.copy(inputFile); } }
Serialization Definition: Supported by the Java Virtual Machine (JVM) Ability to read or write an object to a stream Process of &quot;flattening&quot; an object Goal: To save object to some permanent storage or to pass on to another object via the  OutputStream  class Writing an object: Its state should be written in a serialized form such that the object can be reconstructed as it is being read  Persistence Saving an object to some type of permanent storage
Serialization Streams for serialization ObjectInputStream For deserializing ObjectOutputStream For serializing To allow an object to be serializable: Its class should implement the  Serializable  interface Its class should also provide a default constructor or a constructor with no arguments Serializability is inherited Don't have to implement  Serializable  on every class Can just implement  Serializable  once along the class heirarchy
Non-Serializable Objects When an object is serialized: Only the object's data are preserved Methods and constructors are not part of the serialized stream Some objects are not serializable  Because the data they represent constantly changes Examples: FileInputStream  objects Thread  objects A  NotSerializableException  is thrown if the serialization fails
The  transient  Keyword A class containing a non-serializable object can still be serialized Reference to non-serializable object is marked with the transient keyword Example: class MyClass  implements Serializable  { transient Thread thread; //try removing transient int data; /* some other data */ } The  transient  keyword prevents the data from being serialized
Serialization: Writing an Object Stream Use the  ObjectOutputStream  class Use its  writeObject  method public final void writeObject(Object obj) throws IOException where, obj  is the object to be written to the stream
Serialization: Writing an Object Stream import java.io.*; public class SerializeBoolean { SerializeBoolean() { Boolean booleanData = new Boolean(&quot;true&quot;); try { FileOutputStream fos = new  FileOutputStream(&quot;boolean.ser&quot;); ObjectOutputStream oos = new  ObjectOutputStream(fos); oos.writeObject(booleanData); oos.close(); //continued...
Serialization: Writing an Object Stream } catch (IOException ie) { ie.printStackTrace(); } } public static void main(String args[]) { SerializeBoolean sb = new SerializeBoolean(); } }
Deserialization: Reading an Object Stream Use the  ObjectInputStream  class Use its  readObject  method public final Object readObject()  throws IOException, ClassNotFoundException where, obj  is the object to be read from the stream The  Object  type returned should be typecasted to the appropriate class name before methods on that class can be executed
Deserialization: Reading an Object Stream import java.io.*; public class UnserializeBoolean { UnserializeBoolean() { Boolean booleanData = null; try { FileInputStream fis = new  FileInputStream(&quot;boolean.ser&quot;); ObjectInputStream ois = new  ObjectInputStream(fis); booleanData = (Boolean) ois.readObject(); ois.close(); //continued...
Deserialization: Reading an Object Stream } catch (Exception e) { e.printStackTrace(); } System.out.println(&quot;Unserialized Boolean from &quot;  + &quot;boolean.ser&quot;); System.out.println(&quot;Boolean data: &quot; +  booleanData ); System.out.println(&quot;Compare data with true: &quot; +  booleanData.equals(new Boolean(&quot;true&quot;)) ); } //continued...
Deserialization: Reading an Object Stream public static void main(String args[]) { UnserializeBoolean usb =  new UnserializeBoolean(); } }
Summary General Stream Types Character and Byte Streams Input and Output Streams Node and Filter Streams The  File  Class Constructor File(String pathname) Methods
Summary Reader  Classes Methods read ,  close ,  mark ,  markSupported ,  reset Node  Reader  Classes FileReader ,  CharArrayReader ,  StringReader ,  PipedReader Filter  Reader  Classes BufferedReader ,  FilterReader ,  InputStreamReader , LineNumberReader ,  PushbackReader
Summary Writer  Classes Methods write ,  close ,  flush Node  Writer  Classes FileWriter ,  CharArrayWriter ,  StringWriter ,  PipedWriter Filter  Writer  Classes BufferedWriter ,  FilterWriter ,  OutputStreamWriter ,  PrintWriter
Summary InputStream  Classes Methods read ,  close ,  mark ,  markSupported ,  reset Node  InputStream  Classes FileInputStream ,  BufferedArrayInputStream ,  PipedInputStream Filter  InputStream  Classes BufferedInputStream ,  FilterInputStream ,  ObjectInputStream ,  DataInputStream ,  LineNumberInputStream ,  PushbackInputStream
Summary OutputStream  Classes Methods write ,  close ,  flush Node  OutputStream  Classes FileOutputStream ,  BufferedArrayOutputStream ,  PipedOutputStream Filter  OutputStream  Classes BufferedOutputStream ,  FilterOutputStream ,  ObjectOutputStream ,  DataOutputStream ,  PrintStream
Summary Serialization Definition The  transient  Keyword Serialization: Writing an Object Stream Use the  ObjectOutputStream  class Use its  writeObject  method Deserialization: Reading an Object Stream Use the  ObjectInputStream  class Use its  readObject  method
Ad

More Related Content

What's hot (20)

Java Streams
Java StreamsJava Streams
Java Streams
M Vishnuvardhan Reddy
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
Java IO
Java IOJava IO
Java IO
UTSAB NEUPANE
 
32.java input-output
32.java input-output32.java input-output
32.java input-output
santosh mishra
 
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 Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Input output streams
Input output streamsInput output streams
Input output streams
Parthipan Parthi
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
Hiranya Jayathilaka
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
Marcello Thiry
 
Java file
Java fileJava file
Java file
sonnetdp
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
NilaNila16
 
IO and serialization
IO and serializationIO and serialization
IO and serialization
backdoor
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
Nem Sothea
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
sharma230399
 
Javaiostream
JavaiostreamJavaiostream
Javaiostream
Tien Nguyen
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
myrajendra
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
babak danyal
 
Java I/O
Java I/OJava I/O
Java I/O
Jayant Dalvi
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
teach4uin
 
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 Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
java.io - streams and files
java.io - streams and filesjava.io - streams and files
java.io - streams and files
Marcello Thiry
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
NilaNila16
 
IO and serialization
IO and serializationIO and serialization
IO and serialization
backdoor
 
Files & IO in Java
Files & IO in JavaFiles & IO in Java
Files & IO in Java
CIB Egypt
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
Nem Sothea
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
sharma230399
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
myrajendra
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
babak danyal
 

Similar to Jedi Slides Intro2 Chapter12 Advanced Io Streams (20)

Java IO Stream, the introduction to Streams
Java IO Stream, the introduction to StreamsJava IO Stream, the introduction to Streams
Java IO Stream, the introduction to Streams
ranganadh6
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
Vince Vo
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
Basic input-output-v.1.1
Basic input-output-v.1.1Basic input-output-v.1.1
Basic input-output-v.1.1
BG Java EE Course
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
Riccardo Cardin
 
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
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
People Strategists
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdfMonhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
VithalReddy3
 
15. text files
15. text files15. text files
15. text files
Konstantin Potemichev
 
chapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and streamchapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and stream
amarehope21
 
5java Io
5java Io5java Io
5java Io
Adil Jafri
 
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
phanleson
 
Java 3 Computer Science.pptx
Java 3 Computer Science.pptxJava 3 Computer Science.pptx
Java 3 Computer Science.pptx
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
IO and threads Java
IO and threads JavaIO and threads Java
IO and threads Java
MUHAMMED MASHAHIL PUKKUNNUMMAL
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
PawanMM
 
Java IO Stream, the introduction to Streams
Java IO Stream, the introduction to StreamsJava IO Stream, the introduction to Streams
Java IO Stream, the introduction to Streams
ranganadh6
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
Vince Vo
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
Kavitha713564
 
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
11_Str11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.11_Streams.eams.pdf
hungvidien123
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
sotlsoc
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
SudhanshiBakre1
 
Java - Processing input and output
Java - Processing input and outputJava - Processing input and output
Java - Processing input and output
Riccardo Cardin
 
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
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
cherryreddygannu
 
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdfMonhocvecaujahetvagiuplaptunhhayhonha.pdf
Monhocvecaujahetvagiuplaptunhhayhonha.pdf
cuchuoi83ne
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
VithalReddy3
 
chapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and streamchapter 2(IO and stream)/chapter 2, IO and stream
chapter 2(IO and stream)/chapter 2, IO and stream
amarehope21
 
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
phanleson
 
Session 22 - Java IO, Serialization
Session 22 - Java IO, SerializationSession 22 - Java IO, Serialization
Session 22 - Java IO, Serialization
PawanMM
 
Ad

More from Don Bosco BSIT (20)

Probability and statistics (frequency distributions)
Probability and statistics (frequency distributions)Probability and statistics (frequency distributions)
Probability and statistics (frequency distributions)
Don Bosco BSIT
 
Probability and statistics (basic statistical concepts)
Probability and statistics (basic statistical concepts)Probability and statistics (basic statistical concepts)
Probability and statistics (basic statistical concepts)
Don Bosco BSIT
 
Factors in assembling personal computer
Factors in assembling personal computerFactors in assembling personal computer
Factors in assembling personal computer
Don Bosco BSIT
 
Alumni response
Alumni responseAlumni response
Alumni response
Don Bosco BSIT
 
Summative Report: 1st Consultative Curriculum Dev Oct. 20-21
Summative Report: 1st Consultative Curriculum Dev Oct. 20-21Summative Report: 1st Consultative Curriculum Dev Oct. 20-21
Summative Report: 1st Consultative Curriculum Dev Oct. 20-21
Don Bosco BSIT
 
Data communication basics
Data communication basicsData communication basics
Data communication basics
Don Bosco BSIT
 
Data communication basics
Data communication basicsData communication basics
Data communication basics
Don Bosco BSIT
 
Research Primer
Research PrimerResearch Primer
Research Primer
Don Bosco BSIT
 
Os Virtualization
Os VirtualizationOs Virtualization
Os Virtualization
Don Bosco BSIT
 
SYSAD323 Virtualization Basics
SYSAD323 Virtualization BasicsSYSAD323 Virtualization Basics
SYSAD323 Virtualization Basics
Don Bosco BSIT
 
Shell Scripting Structured Commands
Shell Scripting Structured CommandsShell Scripting Structured Commands
Shell Scripting Structured Commands
Don Bosco BSIT
 
V Communication Error Detection And Correction
V  Communication Error Detection And CorrectionV  Communication Error Detection And Correction
V Communication Error Detection And Correction
Don Bosco BSIT
 
Iv The Telephone And Multiplex Systems
Iv  The Telephone And Multiplex SystemsIv  The Telephone And Multiplex Systems
Iv The Telephone And Multiplex Systems
Don Bosco BSIT
 
Iii Data Transmission Fundamentals
Iii  Data Transmission FundamentalsIii  Data Transmission Fundamentals
Iii Data Transmission Fundamentals
Don Bosco BSIT
 
Ii Communications Channel
Ii   Communications ChannelIi   Communications Channel
Ii Communications Channel
Don Bosco BSIT
 
I Introduction To Data Communications
I  Introduction To Data CommunicationsI  Introduction To Data Communications
I Introduction To Data Communications
Don Bosco BSIT
 
Secondary Storage Device Magnetic Tapes
Secondary Storage Device  Magnetic TapesSecondary Storage Device  Magnetic Tapes
Secondary Storage Device Magnetic Tapes
Don Bosco BSIT
 
Lecture #1 Introduction
Lecture #1 IntroductionLecture #1 Introduction
Lecture #1 Introduction
Don Bosco BSIT
 
Fundamental File Processing Operations
Fundamental File Processing OperationsFundamental File Processing Operations
Fundamental File Processing Operations
Don Bosco BSIT
 
Probability and statistics (frequency distributions)
Probability and statistics (frequency distributions)Probability and statistics (frequency distributions)
Probability and statistics (frequency distributions)
Don Bosco BSIT
 
Probability and statistics (basic statistical concepts)
Probability and statistics (basic statistical concepts)Probability and statistics (basic statistical concepts)
Probability and statistics (basic statistical concepts)
Don Bosco BSIT
 
Factors in assembling personal computer
Factors in assembling personal computerFactors in assembling personal computer
Factors in assembling personal computer
Don Bosco BSIT
 
Summative Report: 1st Consultative Curriculum Dev Oct. 20-21
Summative Report: 1st Consultative Curriculum Dev Oct. 20-21Summative Report: 1st Consultative Curriculum Dev Oct. 20-21
Summative Report: 1st Consultative Curriculum Dev Oct. 20-21
Don Bosco BSIT
 
Data communication basics
Data communication basicsData communication basics
Data communication basics
Don Bosco BSIT
 
Data communication basics
Data communication basicsData communication basics
Data communication basics
Don Bosco BSIT
 
SYSAD323 Virtualization Basics
SYSAD323 Virtualization BasicsSYSAD323 Virtualization Basics
SYSAD323 Virtualization Basics
Don Bosco BSIT
 
Shell Scripting Structured Commands
Shell Scripting Structured CommandsShell Scripting Structured Commands
Shell Scripting Structured Commands
Don Bosco BSIT
 
V Communication Error Detection And Correction
V  Communication Error Detection And CorrectionV  Communication Error Detection And Correction
V Communication Error Detection And Correction
Don Bosco BSIT
 
Iv The Telephone And Multiplex Systems
Iv  The Telephone And Multiplex SystemsIv  The Telephone And Multiplex Systems
Iv The Telephone And Multiplex Systems
Don Bosco BSIT
 
Iii Data Transmission Fundamentals
Iii  Data Transmission FundamentalsIii  Data Transmission Fundamentals
Iii Data Transmission Fundamentals
Don Bosco BSIT
 
Ii Communications Channel
Ii   Communications ChannelIi   Communications Channel
Ii Communications Channel
Don Bosco BSIT
 
I Introduction To Data Communications
I  Introduction To Data CommunicationsI  Introduction To Data Communications
I Introduction To Data Communications
Don Bosco BSIT
 
Secondary Storage Device Magnetic Tapes
Secondary Storage Device  Magnetic TapesSecondary Storage Device  Magnetic Tapes
Secondary Storage Device Magnetic Tapes
Don Bosco BSIT
 
Lecture #1 Introduction
Lecture #1 IntroductionLecture #1 Introduction
Lecture #1 Introduction
Don Bosco BSIT
 
Fundamental File Processing Operations
Fundamental File Processing OperationsFundamental File Processing Operations
Fundamental File Processing Operations
Don Bosco BSIT
 
Ad

Recently uploaded (20)

*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
Arshad Shaikh
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
*"The Segmented Blueprint: Unlocking Insect Body Architecture"*.pptx
Arshad Shaikh
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Cultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptxCultivation Practice of Turmeric in Nepal.pptx
Cultivation Practice of Turmeric in Nepal.pptx
UmeshTimilsina1
 
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptxU3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
U3 ANTITUBERCULAR DRUGS Pharmacology 3.pptx
Mayuri Chavan
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18How to Configure Scheduled Actions in odoo 18
How to Configure Scheduled Actions in odoo 18
Celine George
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
How to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 SalesHow to Manage Upselling in Odoo 18 Sales
How to Manage Upselling in Odoo 18 Sales
Celine George
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 

Jedi Slides Intro2 Chapter12 Advanced Io Streams

  • 1. 12 Advanced I/O Streams
  • 2. Topics General Stream Types Character and Byte Streams Input and Output Streams Node and Filter Streams The File Class Reader Classes Reader Methods Node Reader Classes Filter Reader Classes
  • 3. Topics Writer Classes Writer Methods Node Writer Classes Filter Writer Classes InputStream Classes InputStream Methods Node InputStream Classes Filter InputStream Classes
  • 4. Topics OutputStream Classes OutputStream Methods Node OutputStream Classes Filter OutputStream Classes Serialization The transient Keyword Serialization: Writing an Object Stream Deserialization: Reading an Object Stream
  • 5. General Stream Types Streams Abstraction of a file or a device that allows a series of items to be read or written General Stream Categories Character and Byte Streams Input and Output Streams Node and Filter Streams
  • 6. Character and Byte Streams Character streams File or device abstractions for Unicode characters Superclass of all classes for character streams: The Reader class The Writer class Both classes are abstract Byte streams For binary data Root classes for byte streams: The InputStream Class The OutputStream Class Both classes are abstract
  • 7. Input and Output Streams Input or source streams Can read from these streams Superclasses of all input streams: The InputStream Class The Reader Class Output or sink streams Can write to these streams Root classes of all output streams: The OutputStream Class The Writer Class
  • 8. Node and Filter Streams Node streams Contain the basic functionality of reading or writing from a specific location Types of node streams include files, memory and pipes Filter streams Layered onto node streams between threads or processes For additional functionalities Adding layers to a node stream is called stream chaining
  • 9. The File Class Not a stream class Important since stream classes manipulate File objects Abstract representation of actual files and directory pathnames
  • 10. The File Class: Constructors Has four constructors
  • 11. The File Class: Methods
  • 12. The File Class: Methods
  • 13. The File Class: Example import java.io.*; public class FileInfoClass { public static void main(String args[]) { String fileName = args[0]; File fn = new File(fileName); System.out.println(&quot;Name: &quot; + fn.getName()); if (!fn.exists()) { System.out.println(fileName + &quot; does not exists.&quot;); //continued...
  • 14. The File Class: Example /* Create a temporary directory instead. */ System.out.println(&quot;Creating temp directory...&quot;); fileName = &quot;temp&quot;; fn = new File(fileName); fn.mkdir(); System.out.println(fileName + (fn.exists()? &quot;exists&quot;: &quot;does not exist&quot;)); System.out.println(&quot;Deleting temp directory...&quot;); fn.delete(); //continued...
  • 15. The File Class: Example System.out.println(fileName + (fn.exists()? &quot;exists&quot;: &quot;does not exist&quot;)); return; } //end of: if (!fn.exists()) System.out.println(fileName + &quot; is a &quot; + (fn.isFile()? &quot;file.&quot; :&quot;directory.&quot;)); if (fn.isDirectory()) { String content[] = fn.list(); System.out.println(&quot;The content of this directory:&quot;); //continued...
  • 16. The File Class: Example for (int i = 0; i < content.length; i++) { System.out.println(content[i]); } } //end of: if (fn.isDirectory()) if (!fn.canRead()) { System.out.println(fileName + &quot; is not readable.&quot;); return; } //continued...
  • 17. The File Class: Example System.out.println(fileName + &quot; is &quot; + fn.length() + &quot; bytes long.&quot;); System.out.println(fileName + &quot; is &quot; + fn.lastModified() + &quot; bytes long.&quot;); if (!fn.canWrite()) { System.out.println(fileName + &quot; is not writable.&quot;); } } }
  • 18. The Reader Class: Methods
  • 19. The Reader Class: Methods
  • 20. Node Reader Classes
  • 21. Filter Reader Classes
  • 22. The Writer Class: Methods
  • 23. Node Writer Classes
  • 24. Filter Writer Classes
  • 25. Basic Reader / Writer Example import java.io.*; class CopyFile { void copy(String input, String output) { FileReader reader; FileWriter writer; int data; try { reader = new FileReader(input); writer = new FileWriter(output); //continued...
  • 26. Basic Reader / Writer Example while ((data = reader.read() ) != -1) { writer.write(data); } reader.close(); writer.close(); } catch (IOException ie) { ie.printStackTrace(); } } //continued...
  • 27. Basic Reader / Writer Example public static void main(String args[]) { String inputFile = args[0]; String outputFile = args[1]; CopyFile cf = new CopyFile(); cf.copy(inputFile, outputFile); } }
  • 28. Modified Reader / Writer Example import java.io.*; class CopyFile { void copy(String input, String output) { BufferedReader reader; BufferedWriter writer; String data; try { reader = new BufferedReader(new FileReader(input)); writer = new BufferedWriter(new FileWriter(output)); //continued...
  • 29. Modified Reader / Writer Example while ((data = reader.readLine() ) != null) { writer.write(data, 0, data.length); } reader.close(); writer.close(); } catch (IOException ie) { ie.printStackTrace(); } } //continued...
  • 30. Modified Reader / Writer Example public static void main(String args[]) { String inputFile = args[0]; String outputFile = args[1]; CopyFile cf = new CopyFile(); cf.copy(inputFile, outputFile); } }
  • 31. The InputStream Class: Methods
  • 32. The InputStream Class: Methods
  • 33. Node InputStream Classes
  • 35. The OutputStream Class: Methods
  • 36. Node OutputStream Classes
  • 38. Basic InputStream / OutputStream Example import java.io.*; class CopyFile { void copy(String input, String output) { FileInputStream inputStr; FileOutputStream outputStr; int data; try { inputStr = new FileInputStream(input); outputStr = new FileOutputStream(output); //continued...
  • 39. Basic InputStream / OutputStream Example while ((data = inputStr.read() ) != -1) { outputStr.write(data); } inputStr.close(); outputStr.close(); } catch (IOException ie) { ie.printStackTrace(); } } //continued...
  • 40. Basic InputStream / OutputStream Example public static void main(String args[]) { String inputFile = args[0]; String outputFile = args[1]; CopyFile cf = new CopyFile(); cf.copy(inputFile, outputFile); } }
  • 41. Modified InputStream / OutputStream Example import java.io.*; class CopyFile { void copy(String input) { PushbackInputStream inputStr; PrintStream outputStr; int data; try { inputStr = new PushbackInputStream(new FileInputStream(input)); outputStr = new PrintStream(System.out); //continued...
  • 42. Modified InputStream / OutputStream Example while ((data = inputStr.read() ) != -1) { outputStr.println(&quot;read data: &quot; + (char) data); inputStr.unread(data); data = inputStr.read(); outputStr.println(&quot;unread data: &quot; + (char) data); } inputStr.close(); outputStr.close(); //continued...
  • 43. Modified InputStream / OutputStream Example } catch (IOException ie) { ie.printStackTrace(); } } public static void main(String args[]) { String inputFile = args[0]; CopyFile cf = new CopyFile(); cf.copy(inputFile); } }
  • 44. Serialization Definition: Supported by the Java Virtual Machine (JVM) Ability to read or write an object to a stream Process of &quot;flattening&quot; an object Goal: To save object to some permanent storage or to pass on to another object via the OutputStream class Writing an object: Its state should be written in a serialized form such that the object can be reconstructed as it is being read Persistence Saving an object to some type of permanent storage
  • 45. Serialization Streams for serialization ObjectInputStream For deserializing ObjectOutputStream For serializing To allow an object to be serializable: Its class should implement the Serializable interface Its class should also provide a default constructor or a constructor with no arguments Serializability is inherited Don't have to implement Serializable on every class Can just implement Serializable once along the class heirarchy
  • 46. Non-Serializable Objects When an object is serialized: Only the object's data are preserved Methods and constructors are not part of the serialized stream Some objects are not serializable Because the data they represent constantly changes Examples: FileInputStream objects Thread objects A NotSerializableException is thrown if the serialization fails
  • 47. The transient Keyword A class containing a non-serializable object can still be serialized Reference to non-serializable object is marked with the transient keyword Example: class MyClass implements Serializable { transient Thread thread; //try removing transient int data; /* some other data */ } The transient keyword prevents the data from being serialized
  • 48. Serialization: Writing an Object Stream Use the ObjectOutputStream class Use its writeObject method public final void writeObject(Object obj) throws IOException where, obj is the object to be written to the stream
  • 49. Serialization: Writing an Object Stream import java.io.*; public class SerializeBoolean { SerializeBoolean() { Boolean booleanData = new Boolean(&quot;true&quot;); try { FileOutputStream fos = new FileOutputStream(&quot;boolean.ser&quot;); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(booleanData); oos.close(); //continued...
  • 50. Serialization: Writing an Object Stream } catch (IOException ie) { ie.printStackTrace(); } } public static void main(String args[]) { SerializeBoolean sb = new SerializeBoolean(); } }
  • 51. Deserialization: Reading an Object Stream Use the ObjectInputStream class Use its readObject method public final Object readObject() throws IOException, ClassNotFoundException where, obj is the object to be read from the stream The Object type returned should be typecasted to the appropriate class name before methods on that class can be executed
  • 52. Deserialization: Reading an Object Stream import java.io.*; public class UnserializeBoolean { UnserializeBoolean() { Boolean booleanData = null; try { FileInputStream fis = new FileInputStream(&quot;boolean.ser&quot;); ObjectInputStream ois = new ObjectInputStream(fis); booleanData = (Boolean) ois.readObject(); ois.close(); //continued...
  • 53. Deserialization: Reading an Object Stream } catch (Exception e) { e.printStackTrace(); } System.out.println(&quot;Unserialized Boolean from &quot; + &quot;boolean.ser&quot;); System.out.println(&quot;Boolean data: &quot; + booleanData ); System.out.println(&quot;Compare data with true: &quot; + booleanData.equals(new Boolean(&quot;true&quot;)) ); } //continued...
  • 54. Deserialization: Reading an Object Stream public static void main(String args[]) { UnserializeBoolean usb = new UnserializeBoolean(); } }
  • 55. Summary General Stream Types Character and Byte Streams Input and Output Streams Node and Filter Streams The File Class Constructor File(String pathname) Methods
  • 56. Summary Reader Classes Methods read , close , mark , markSupported , reset Node Reader Classes FileReader , CharArrayReader , StringReader , PipedReader Filter Reader Classes BufferedReader , FilterReader , InputStreamReader , LineNumberReader , PushbackReader
  • 57. Summary Writer Classes Methods write , close , flush Node Writer Classes FileWriter , CharArrayWriter , StringWriter , PipedWriter Filter Writer Classes BufferedWriter , FilterWriter , OutputStreamWriter , PrintWriter
  • 58. Summary InputStream Classes Methods read , close , mark , markSupported , reset Node InputStream Classes FileInputStream , BufferedArrayInputStream , PipedInputStream Filter InputStream Classes BufferedInputStream , FilterInputStream , ObjectInputStream , DataInputStream , LineNumberInputStream , PushbackInputStream
  • 59. Summary OutputStream Classes Methods write , close , flush Node OutputStream Classes FileOutputStream , BufferedArrayOutputStream , PipedOutputStream Filter OutputStream Classes BufferedOutputStream , FilterOutputStream , ObjectOutputStream , DataOutputStream , PrintStream
  • 60. Summary Serialization Definition The transient Keyword Serialization: Writing an Object Stream Use the ObjectOutputStream class Use its writeObject method Deserialization: Reading an Object Stream Use the ObjectInputStream class Use its readObject method
  翻译: