SlideShare a Scribd company logo
1 of 24
 Understand the stream
 Know the difference between byte and
  character streams
 Know Java’s byte stream classes
 Know Java’s character stream classes
 Know the predefined streams
 Use byte streams
 Use byte streams for file I/O
 Read and write binary data
 Use character streams
 Use character streams for file I/O
 Object Serialization
 Object Deserialization
A stream is an abstract representation of an input or output device that is a source of,
or destination for, data. You can write data to a stream and read data from a stream.
You can visualize a stream as a sequence of bytes that flows into or out of your
program

Java programs perform I/O through streams.

A stream is an abstraction that either produces or consumes information. A stream is
 linked to a physical device by the Java I/O system.
All streams behave in the same manner, even if the actual physical devices they are
linked to differ.

Thus, the same I/O classes and methods can be applied to any type of device.
For example, the same methods that you use to write to the console can also be used
to write to a disk file. Java implements streams within class hierarchies defined in the
java.io package.
Modern versions of Java define two types of Streams: byte and
character.
Byte streams provide a convenient means for handling input and
output of bytes.
Byte streams are used, for example, when reading or writing
binary data. They are especially helpful when working with files.
Character streams are designed for handling the input and
output of characters.
Character streams use Unicode and, therefore, can be
internationalized. Also, in some cases, character streams are
 more efficient than byte streams.
At the lowest level, all I/O is still byte-oriented. The character-based
streams simply provide a convenient and efficient means for handling
characters.
Byte streams are defined by using two class hierarchies.
At the top of these are two abstract classes:
InputStream and OutputStream.
InputStream defines the characteristics common to byte
input streams and OutputStream describes the behavior of
byte output streams.
From InputStream and OutputStream are created several
concrete subclasses that offer varying functionality and
handle the details of reading and writing to various devices,
such as disk files.
Character streams are defined by using two class
hierarchies topped by these two abstract classes:
  Reader and Writer.
Reader is used for input, and Writer is used for
output.

Concrete classes derived from Reader and Writer
operate on Unicode character streams.

From Reader and Writer are derived several
concrete subclasses that handle various I/O
situations. In general, the character-based classes
parallel the byte-based classes.
As you know, all Java programs automatically import the
java.lang package. This package defines a class called System,
which encapsulates several aspects of the run-time environment.
Among other things, it contains three predefined stream variables,
called in, out, and err.

These fields are declared as public and static within System.
This means that they can be used by any other part of your
program and without reference to a specific System object.

System.out refers to the standard output stream. By default,
this is the console. System.in refers to standard input, which is
by default the keyboard.
System.err refers to the standard error stream, which is also the
console by default. However, these streams can be redirected to
any compatible I/O device.
   BufferedInputStream        InputStream
   BufferedOutputStream       ObjectInputStream
   ByteArrayInputStream       ObjectOutputStream
   ByteArrayOutputStream      OutputStream
   DataInputStream            PipedInputStream
   DataOutputStream           PipedOutputStream
   FileInputStream            PrintStream
   FileOutputStream           PushbackInputStream
   FilterInputStream          RandomAccessFile
   FilterOutputStream         SequenceInputStream
   BufferedReader         LineNumberReader
   BufferedWriter         OutputStreamWriter
   CharArrayReader        PipedReader
   CharArrayWriter        PipedWriter
   FileReader             PrintWriter
   FileWriter             PushbackReader
   FilterReader           Reader
   FilterWriter           StringReader
   InputStreamReader      StringWriter
import java.io.*;
class ReadBytes {
       public static void main(String args[]) throws
  Exception {
              byte data[] = new byte[10];
              System.out.println("Enter some characters.");
              System.in.read(data);
              System.out.print("You entered: ");
              for(int i=0; i < data.length; i++)
                     System.out.print((char) data[i]);
                     }
              }
class WriteDemo {
       public static void main(String args[]) {
              int b;
              b = 'X';
              System.out.write(b);
              System.out.write('n');
              }
  }
import java.io.*;
class ShowFile {
         public static void main(String args[])
         throws IOException {
                  int i;
                  FileInputStream fin = new FileInputStream(args[0]);


                 do {
                 i = fin.read();
                 if(i != -1) System.out.print((char) i);
                 } while(i != -1);

                 fin.close();
                 }
  }
import java.io.*;
class CopyFile {
         public static void main(String args[]) throws Exception {
         int i;
         FileInputStream fin = new FileInputStream(args[0]);
         FileOutputStream fout = new FileOutputStream(args[1]);

        do {
                 i = fin.read();
                 if(i != -1) fout.write(i);
        } while(i != -1);
        fin.close();
        fout.close();
        }
}
import java.io.*;
class RWData {
      public static void main(String args[]) throws Exception {
      DataOutputStream dataOut = new DataOutputStream(new
FileOutputStream("testdata"));
      int i = 10;
      double d = 1023.56;
      boolean b = true;
      System.out.println("Writing " + i); dataOut.writeInt(i);
      System.out.println("Writing " + d); dataOut.writeDouble(d);
      System.out.println("Writing " + b); dataOut.writeBoolean(b);
      System.out.println("Writing " + 12.2 * 7.4); dataOut.writeDouble(12.2 * 7.4);
      dataOut.close();
      System.out.println();
      DataInputStream dataIn = new DataInputStream(new FileInputStream("testdata"));
      i = dataIn.readInt(); System.out.println("Reading " + i);
      d = dataIn.readDouble(); System.out.println("Reading " + d);
      b = dataIn.readBoolean(); System.out.println("Reading " + b);
      d = dataIn.readDouble(); System.out.println("Reading " + d);
      dataIn.close();
      }
}
import java.io.*;
class CompFiles {
         public static void main(String args[])throws Exception {
         int i=0, j=0;
         FileInputStream f1 = new FileInputStream(args[0]);

        FileInputStream f2 = new FileInputStream(args[1]);
        do {
        i = f1.read();
        j = f2.read();
                 if(i != j) break;
        } while(i != -1 && j != -1);

        if(i != j)
                 System.out.println("Files differ.");
        else
                 System.out.println("Files are the same.");
        f1.close();
        f2.close();
        }
}
import java.io.*;
class ReadChars {

public static void main(String args[]) throws Exception {
char c;
BufferedReader br = new BufferedReader(
                   new InputStreamReader(System.in));
System.out.println("Enter characters, period to quit.");

do {
c = (char) br.read();
System.out.println(c);
} while(c != '.');
}
}
import java.io.*;
Create BufferedReader
class ReadLines {
public static void main(String args[]) Throws Exception {
BufferedReader br = new BufferedReader(new
                         InputStreamReader(System.in));
String str;
System.out.println("Enter lines of text.");
System.out.println("Enter 'stop' to quit.");
do {
str = br.readLine();
System.out.println(str);
} while(!str.equals("stop"));
}
}
import java.io.*;
public class PrintWriterDemo {
public static void main(String args[]) {
PrintWriter pw = new PrintWriter(System.out, true);
int i = 10;
double d = 123.65;
pw.println("Using a PrintWriter.");
pw.println(i);
pw.println(d);
pw.println(i + " + " + d + " is " + (i+d));
}
}
import java.io.*;
class KtoD {
public static void main(String args[]) throws Exception {
String str;
FileWriter fw = new FileWriter("test.txt");
BufferedReader br = new BufferedReader( new InputStreamReader(System.in));
System.out.println("Enter text ('stop' to quit).");
do {
System.out.print(": "); str = br.readLine();
if(str.compareTo("stop") == 0) break;
str = str + "rn"; // add newline
fw.write(str);
} while(str.compareTo("stop") != 0);
fw.close();
}
}
import java.io.*;

class DtoS {

public static void main(String args[]) throws Exception {

FileReader fr = new FileReader("test.txt");

BufferedReader br = new BufferedReader(fr);

String s;

while((s = br.readLine()) != null) {

System.out.println(s);

}

fr.close();

}

}
class MyClass implements Serializable {
         double id;
         String name;
         String course;


    public MyClass(double i, String n, String c) {
    id=i; name=n;course=c;
    }
    public String toString() {
    return “ID: " + id + “nNAME: " + name + “nCOURSE: "+ course;
    }
}
import java.io.*;
    public class SerializationDemo {
         public static void main(String args[]) throws Exception{
// Object serialization
      MyClass object1 = new MyClass(2342, “Shahjahan”, “Java”);
    System.out.println("object1: " + object1);
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
// Object deserialization
MyClass object2;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
object2 = (MyClass)ois.readObject();
ois.close();
System.out.println("object2: " + object2);
}
}
import java.util.zip.*;

import java.io.*;

public class GZIPcompress {

public static void main(String[] args)throws IOException {

BufferedReader in = new BufferedReader( new FileReader(args[0]));

BufferedOutputStream out = new BufferedOutputStream( new GZIPOutputStream(

new FileOutputStream("test.gz")));

int c; while((c = in.read()) != -1) out.write(c);

in.close(); out.close();

BufferedReader in2 = new BufferedReader( new InputStreamReader(new

    GZIPInputStream( new FileInputStream("test.gz"))));

String s; while((s = in2.readLine()) != null) System.out.println(s);

}

}

More Related Content

What's hot

Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And MultithreadingShraddha
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)Om Ganesh
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket ProgrammingVipin Yadav
 
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 StreamsAnton Keks
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streamsbabak danyal
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to javaAjay Sharma
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)arvind pandey
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Michelle Anne Meralpis
 
Constructor in java
Constructor in javaConstructor in java
Constructor in javaHitesh Kumar
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methodsShubham Dwivedi
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaCPD INDIA
 

What's hot (20)

Java And Multithreading
Java And MultithreadingJava And Multithreading
Java And Multithreading
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Java Socket Programming
Java Socket ProgrammingJava Socket Programming
Java Socket Programming
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java constructors
Java constructorsJava constructors
Java constructors
 
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 Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Threads in JAVA
Threads in JAVAThreads in JAVA
Threads in JAVA
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java IO Package and Streams
Java IO Package and StreamsJava IO Package and Streams
Java IO Package and Streams
 
Introduction to java
Introduction to javaIntroduction to java
Introduction to java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Core java complete ppt(note)
Core java  complete  ppt(note)Core java  complete  ppt(note)
Core java complete ppt(note)
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java abstract class & abstract methods
Java abstract class & abstract methodsJava abstract class & abstract methods
Java abstract class & abstract methods
 
oops concept in java | object oriented programming in java
oops concept in java | object oriented programming in javaoops concept in java | object oriented programming in java
oops concept in java | object oriented programming in java
 

Viewers also liked

Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File HandlingSunil OS
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49myrajendra
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Java I/O and Object Serialization
Java I/O and Object SerializationJava I/O and Object Serialization
Java I/O and Object SerializationNavneet Prakash
 
Java8 javatime-api
Java8 javatime-apiJava8 javatime-api
Java8 javatime-apiJini Lee
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesGanesh Samarthyam
 
X$Tables And Sga Scanner, DOAG2009
X$Tables And Sga Scanner, DOAG2009X$Tables And Sga Scanner, DOAG2009
X$Tables And Sga Scanner, DOAG2009Frank
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52myrajendra
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28myrajendra
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in javaRicha Singh
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scannerArif Ullah
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Harmeet Singh(Taara)
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructorShivam Singhal
 

Viewers also liked (20)

Java Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 
Byte stream classes.49
Byte stream classes.49Byte stream classes.49
Byte stream classes.49
 
Files in java
Files in javaFiles in java
Files in java
 
Handling I/O in Java
Handling I/O in JavaHandling I/O in Java
Handling I/O in Java
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
5java Io
5java Io5java Io
5java Io
 
Java I/O and Object Serialization
Java I/O and Object SerializationJava I/O and Object Serialization
Java I/O and Object Serialization
 
Java8 javatime-api
Java8 javatime-apiJava8 javatime-api
Java8 javatime-api
 
Java8
Java8Java8
Java8
 
Java 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional InterfacesJava 8 Lambda Built-in Functional Interfaces
Java 8 Lambda Built-in Functional Interfaces
 
Streams in Java 8
Streams in Java 8Streams in Java 8
Streams in Java 8
 
Java 8 Date and Time API
Java 8 Date and Time APIJava 8 Date and Time API
Java 8 Date and Time API
 
X$Tables And Sga Scanner, DOAG2009
X$Tables And Sga Scanner, DOAG2009X$Tables And Sga Scanner, DOAG2009
X$Tables And Sga Scanner, DOAG2009
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
 
Nested classes in java
Nested classes in javaNested classes in java
Nested classes in java
 
Buffer and scanner
Buffer and scannerBuffer and scanner
Buffer and scanner
 
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)Java 8 Streams And Common Operations By Harmeet Singh(Taara)
Java 8 Streams And Common Operations By Harmeet Singh(Taara)
 
java: basics, user input, data type, constructor
java:  basics, user input, data type, constructorjava:  basics, user input, data type, constructor
java: basics, user input, data type, constructor
 

Similar to Understanding java streams

File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptxcherryreddygannu
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdfSudhanshiBakre1
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and OutputEduardo Bergavera
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12Vince Vo
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingGurpreet singh
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3sotlsoc
 
Here is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdfHere is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdfpratyushraj61
 
import java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdfimport java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdfmanojmozy
 
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 V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4Sunil OS
 

Similar to Understanding java streams (20)

Java Programming - 06 java file io
Java Programming - 06 java file ioJava Programming - 06 java file io
Java Programming - 06 java file io
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
File Input and output.pptx
File Input  and output.pptxFile Input  and output.pptx
File Input and output.pptx
 
Lab4
Lab4Lab4
Lab4
 
File Handling in Java.pdf
File Handling in Java.pdfFile Handling in Java.pdf
File Handling in Java.pdf
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Java I/O
Java I/OJava I/O
Java I/O
 
Chapter 12 - File Input and Output
Chapter 12 - File Input and OutputChapter 12 - File Input and Output
Chapter 12 - File Input and Output
 
Java căn bản - Chapter12
Java căn bản - Chapter12Java căn bản - Chapter12
Java căn bản - Chapter12
 
IO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxingIO Streams, Serialization, de-serialization, autoboxing
IO Streams, Serialization, de-serialization, autoboxing
 
Chapter 10.3
Chapter 10.3Chapter 10.3
Chapter 10.3
 
Oodp mod4
Oodp mod4Oodp mod4
Oodp mod4
 
Here is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdfHere is my code for a linefile editor import java.io.BufferedRea.pdf
Here is my code for a linefile editor import java.io.BufferedRea.pdf
 
import java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdfimport java.io.BufferedReader;import java.io.File;import java.io.pdf
import java.io.BufferedReader;import java.io.File;import java.io.pdf
 
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
 
Unit IV Notes.docx
Unit IV Notes.docxUnit IV Notes.docx
Unit IV Notes.docx
 
Java IO Streams V4
Java IO Streams V4Java IO Streams V4
Java IO Streams V4
 
Code red SUM
Code red SUMCode red SUM
Code red SUM
 
Bhaloo
BhalooBhaloo
Bhaloo
 
Files io
Files ioFiles io
Files io
 

Recently uploaded

Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupJonathanParaisoCruz
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 

Recently uploaded (20)

TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
MARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized GroupMARGINALIZATION (Different learners in Marginalized Group
MARGINALIZATION (Different learners in Marginalized Group
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 

Understanding java streams

  • 1.
  • 2.  Understand the stream  Know the difference between byte and character streams  Know Java’s byte stream classes  Know Java’s character stream classes  Know the predefined streams  Use byte streams  Use byte streams for file I/O  Read and write binary data  Use character streams  Use character streams for file I/O  Object Serialization  Object Deserialization
  • 3. A stream is an abstract representation of an input or output device that is a source of, or destination for, data. You can write data to a stream and read data from a stream. You can visualize a stream as a sequence of bytes that flows into or out of your program Java programs perform I/O through streams. A stream is an abstraction that either produces or consumes information. A stream is linked to a physical device by the Java I/O system. All streams behave in the same manner, even if the actual physical devices they are linked to differ. Thus, the same I/O classes and methods can be applied to any type of device. For example, the same methods that you use to write to the console can also be used to write to a disk file. Java implements streams within class hierarchies defined in the java.io package.
  • 4. Modern versions of Java define two types of Streams: byte and character. Byte streams provide a convenient means for handling input and output of bytes. Byte streams are used, for example, when reading or writing binary data. They are especially helpful when working with files. Character streams are designed for handling the input and output of characters. Character streams use Unicode and, therefore, can be internationalized. Also, in some cases, character streams are more efficient than byte streams. At the lowest level, all I/O is still byte-oriented. The character-based streams simply provide a convenient and efficient means for handling characters.
  • 5. Byte streams are defined by using two class hierarchies. At the top of these are two abstract classes: InputStream and OutputStream. InputStream defines the characteristics common to byte input streams and OutputStream describes the behavior of byte output streams. From InputStream and OutputStream are created several concrete subclasses that offer varying functionality and handle the details of reading and writing to various devices, such as disk files.
  • 6. Character streams are defined by using two class hierarchies topped by these two abstract classes: Reader and Writer. Reader is used for input, and Writer is used for output. Concrete classes derived from Reader and Writer operate on Unicode character streams. From Reader and Writer are derived several concrete subclasses that handle various I/O situations. In general, the character-based classes parallel the byte-based classes.
  • 7. As you know, all Java programs automatically import the java.lang package. This package defines a class called System, which encapsulates several aspects of the run-time environment. Among other things, it contains three predefined stream variables, called in, out, and err. These fields are declared as public and static within System. This means that they can be used by any other part of your program and without reference to a specific System object. System.out refers to the standard output stream. By default, this is the console. System.in refers to standard input, which is by default the keyboard. System.err refers to the standard error stream, which is also the console by default. However, these streams can be redirected to any compatible I/O device.
  • 8. BufferedInputStream  InputStream  BufferedOutputStream  ObjectInputStream  ByteArrayInputStream  ObjectOutputStream  ByteArrayOutputStream  OutputStream  DataInputStream  PipedInputStream  DataOutputStream  PipedOutputStream  FileInputStream  PrintStream  FileOutputStream  PushbackInputStream  FilterInputStream  RandomAccessFile  FilterOutputStream  SequenceInputStream
  • 9. BufferedReader  LineNumberReader  BufferedWriter  OutputStreamWriter  CharArrayReader  PipedReader  CharArrayWriter  PipedWriter  FileReader  PrintWriter  FileWriter  PushbackReader  FilterReader  Reader  FilterWriter  StringReader  InputStreamReader  StringWriter
  • 10. import java.io.*; class ReadBytes { public static void main(String args[]) throws Exception { byte data[] = new byte[10]; System.out.println("Enter some characters."); System.in.read(data); System.out.print("You entered: "); for(int i=0; i < data.length; i++) System.out.print((char) data[i]); } }
  • 11. class WriteDemo { public static void main(String args[]) { int b; b = 'X'; System.out.write(b); System.out.write('n'); } }
  • 12. import java.io.*; class ShowFile { public static void main(String args[]) throws IOException { int i; FileInputStream fin = new FileInputStream(args[0]); do { i = fin.read(); if(i != -1) System.out.print((char) i); } while(i != -1); fin.close(); } }
  • 13. import java.io.*; class CopyFile { public static void main(String args[]) throws Exception { int i; FileInputStream fin = new FileInputStream(args[0]); FileOutputStream fout = new FileOutputStream(args[1]); do { i = fin.read(); if(i != -1) fout.write(i); } while(i != -1); fin.close(); fout.close(); } }
  • 14. import java.io.*; class RWData { public static void main(String args[]) throws Exception { DataOutputStream dataOut = new DataOutputStream(new FileOutputStream("testdata")); int i = 10; double d = 1023.56; boolean b = true; System.out.println("Writing " + i); dataOut.writeInt(i); System.out.println("Writing " + d); dataOut.writeDouble(d); System.out.println("Writing " + b); dataOut.writeBoolean(b); System.out.println("Writing " + 12.2 * 7.4); dataOut.writeDouble(12.2 * 7.4); dataOut.close(); System.out.println(); DataInputStream dataIn = new DataInputStream(new FileInputStream("testdata")); i = dataIn.readInt(); System.out.println("Reading " + i); d = dataIn.readDouble(); System.out.println("Reading " + d); b = dataIn.readBoolean(); System.out.println("Reading " + b); d = dataIn.readDouble(); System.out.println("Reading " + d); dataIn.close(); } }
  • 15. import java.io.*; class CompFiles { public static void main(String args[])throws Exception { int i=0, j=0; FileInputStream f1 = new FileInputStream(args[0]); FileInputStream f2 = new FileInputStream(args[1]); do { i = f1.read(); j = f2.read(); if(i != j) break; } while(i != -1 && j != -1); if(i != j) System.out.println("Files differ."); else System.out.println("Files are the same."); f1.close(); f2.close(); } }
  • 16. import java.io.*; class ReadChars { public static void main(String args[]) throws Exception { char c; BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter characters, period to quit."); do { c = (char) br.read(); System.out.println(c); } while(c != '.'); } }
  • 17. import java.io.*; Create BufferedReader class ReadLines { public static void main(String args[]) Throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str; System.out.println("Enter lines of text."); System.out.println("Enter 'stop' to quit."); do { str = br.readLine(); System.out.println(str); } while(!str.equals("stop")); } }
  • 18. import java.io.*; public class PrintWriterDemo { public static void main(String args[]) { PrintWriter pw = new PrintWriter(System.out, true); int i = 10; double d = 123.65; pw.println("Using a PrintWriter."); pw.println(i); pw.println(d); pw.println(i + " + " + d + " is " + (i+d)); } }
  • 19. import java.io.*; class KtoD { public static void main(String args[]) throws Exception { String str; FileWriter fw = new FileWriter("test.txt"); BufferedReader br = new BufferedReader( new InputStreamReader(System.in)); System.out.println("Enter text ('stop' to quit)."); do { System.out.print(": "); str = br.readLine(); if(str.compareTo("stop") == 0) break; str = str + "rn"; // add newline fw.write(str); } while(str.compareTo("stop") != 0); fw.close(); } }
  • 20. import java.io.*; class DtoS { public static void main(String args[]) throws Exception { FileReader fr = new FileReader("test.txt"); BufferedReader br = new BufferedReader(fr); String s; while((s = br.readLine()) != null) { System.out.println(s); } fr.close(); } }
  • 21. class MyClass implements Serializable { double id; String name; String course; public MyClass(double i, String n, String c) { id=i; name=n;course=c; } public String toString() { return “ID: " + id + “nNAME: " + name + “nCOURSE: "+ course; } }
  • 22. import java.io.*; public class SerializationDemo { public static void main(String args[]) throws Exception{ // Object serialization MyClass object1 = new MyClass(2342, “Shahjahan”, “Java”); System.out.println("object1: " + object1); FileOutputStream fos = new FileOutputStream("serial"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(object1); oos.flush(); oos.close(); }
  • 23. // Object deserialization MyClass object2; FileInputStream fis = new FileInputStream("serial"); ObjectInputStream ois = new ObjectInputStream(fis); object2 = (MyClass)ois.readObject(); ois.close(); System.out.println("object2: " + object2); } }
  • 24. import java.util.zip.*; import java.io.*; public class GZIPcompress { public static void main(String[] args)throws IOException { BufferedReader in = new BufferedReader( new FileReader(args[0])); BufferedOutputStream out = new BufferedOutputStream( new GZIPOutputStream( new FileOutputStream("test.gz"))); int c; while((c = in.read()) != -1) out.write(c); in.close(); out.close(); BufferedReader in2 = new BufferedReader( new InputStreamReader(new GZIPInputStream( new FileInputStream("test.gz")))); String s; while((s = in2.readLine()) != null) System.out.println(s); } }