SlideShare a Scribd company logo
1 of 15
ByteStream classes




    http://improvejava.blogspot.in/   1
Objective

On completion of this period, you would be able to
 learn
   • ByteStream classes




                    http://improvejava.blogspot.in/   2
Recap

In the last class, you have studied about the File class

• You have also studied about the various methods,
  and properties of a File class




                    http://improvejava.blogspot.in/        3
Types of Stream Class

Java’s stream-based I/O is built upon four abstract
    classes:
      1.   Input Stream
      2.   Output Stream
      3.   Reader
      4.   Writer




                     http://improvejava.blogspot.in/   4
Byte Streams

Byte Streams are two types

   1.   InputStream class
   2.   OutputStream class




                     http://improvejava.blogspot.in/   5
InputStream class

• Data is accessed as a sequence of bytes
• Its an abstract class that defines streaming byte
  input
• The methods in the input stream are used to read
  bytes from input stream
• All methods in this class will throw an I/O
  Exception



                   http://improvejava.blogspot.in/    6
OutputStream class

• Is an abstract class that defines streaming byte
  output
• The methods of it are used to send bytes to the
  output stream
• All methods in this class return a void value and
  throw an I/O Exception in the case of errors




                    http://improvejava.blogspot.in/   7
FileInputStream class

• Is a subclass of InputStream that you can use to read
  bytes from a file
• Its has two constructors
         • FileInputStream(String filePath)
         • FileInputStream(File fileObj)
• When FileInputStream is created, it is also opened for
  reading
• FileInputStream overrides six of the methods in the
  abstract class InputStream


                    http://improvejava.blogspot.in/       8
// Program for FileInputStream
import java.io.*;
class FileInputStreamDemo {
public static void main(String args[]) throws Exception {
int size;
InputStream f =
new FileInputStream("FileInputStreamDemo.java");
System.out.println("Total Available Bytes: " +
(size = f.available()));
int n = size/40;
System.out.println("First " + n +
" bytes of the file one read() at a time");
for (int i=0; i < n; i++) {
System.out.print((char) f.read()); }
System.out.println("nStill Available: " + f.available());
System.out.println("Reading the next " + n +
" with one read(b[])");
                            http://improvejava.blogspot.in/   9
// Program for FileInputStream
byte b[] = new byte[n];
if (f.read(b) != n) {
System.err.println("couldn't read " + n + " bytes."); }
System.out.println(new String(b, 0, n));
System.out.println("nStill Available: " + (size = f.available()));
System.out.println("Skipping half of remaining bytes with skip()");
f.skip(size/2);
System.out.println("Still Available: " + f.available());
System.out.println("Reading " + n/2 + " into the end of array");
if (f.read(b, n/2, n/2) != n/2) {
System.err.println("couldn't read " + n/2 + " bytes."); }
System.out.println(new String(b, 0, b.length));
System.out.println("nStill Available: " + f.available());
f.close(); } }



                       http://improvejava.blogspot.in/                10
FileOutputStream class

• Is a subclass of OutputStream that you can use to
  write bytes to a file
• Constructors are
   •   FileOutputStream(String filePath)
   •   FileOutputStream(File fileObj)
   •   FileOutputStream(String filePath, boolean append)
   •   FileOutputStream(File fileObj, boolean append)
• They can throw a FileNotFoundException or a
  SecurityException

                       http://improvejava.blogspot.in/     11
Summary

•   Byte Streams are two types: InputStream
    and OutputStream
•   In InputStream data is accessed as a
    sequence of bytes
•   FileInputStream and FilOuputStream are
    the subclasses of ByteStream



                 http://improvejava.blogspot.in/   12
Quiz

1. All methods in InputStream class will throw an
   IOException

   A. True
   B. False




                   http://improvejava.blogspot.in/   13
Quiz                      contd ..

2. Which method is used to read bytes from
   InpuStream

   A.   readLine()
   B.   read()
   C.   write()
   D.   None




                     http://improvejava.blogspot.in/              14
Frequently Asked Questions

1. Write about Byte Stream classes
2. Write a java program which illustrates the use of
   FileInputStream




                   http://improvejava.blogspot.in/     15

More Related Content

What's hot

Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdmHarshal Misalkar
 
Exception Handling
Exception HandlingException Handling
Exception HandlingReddhi Basu
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
String and string buffer
String and string bufferString and string buffer
String and string bufferkamal kotecha
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of ConstructorsDhrumil Panchal
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in javaNilesh Dalvi
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationshipsPooja mittal
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-threadjavaicon
 
File handling in c
File handling in cFile handling in c
File handling in caakanksha s
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compilerSudhaa Ravi
 

What's hot (20)

Abstract class in java
Abstract class in javaAbstract class in java
Abstract class in java
 
Introduction to method overloading &amp; method overriding in java hdm
Introduction to method overloading &amp; method overriding  in java  hdmIntroduction to method overloading &amp; method overriding  in java  hdm
Introduction to method overloading &amp; method overriding in java hdm
 
javathreads
javathreadsjavathreads
javathreads
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Method overloading
Method overloadingMethod overloading
Method overloading
 
Java threads
Java threadsJava threads
Java threads
 
Packages in java
Packages in javaPackages in java
Packages in java
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Constructor and Types of Constructors
Constructor and Types of ConstructorsConstructor and Types of Constructors
Constructor and Types of Constructors
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Object and class relationships
Object and class relationshipsObject and class relationships
Object and class relationships
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
File handling in c
File handling in cFile handling in c
File handling in c
 
Structure of the compiler
Structure of the compilerStructure of the compiler
Structure of the compiler
 

Viewers also liked

Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streamsShahjahan Samoon
 
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
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaAdil Mehmoood
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object ReferencesTareq Hasan
 
Collections Framework
Collections FrameworkCollections Framework
Collections FrameworkSunil OS
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and OperatorsSunil OS
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)Om Ganesh
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Javabackdoor
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/ServletSunil OS
 
Java Basics
Java BasicsJava Basics
Java BasicsSunil OS
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
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
 

Viewers also liked (20)

Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
 
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
 
Inner classes
Inner classesInner classes
Inner classes
 
Inner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in javaInner classes ,annoumous and outer classes in java
Inner classes ,annoumous and outer classes in java
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Java: Objects and Object References
Java: Objects and Object ReferencesJava: Objects and Object References
Java: Objects and Object References
 
Collections Framework
Collections FrameworkCollections Framework
Collections Framework
 
Java Inner Classes
Java Inner ClassesJava Inner Classes
Java Inner Classes
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
JAVA OOP
JAVA OOPJAVA OOP
JAVA OOP
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)Java Input Output (java.io.*)
Java Input Output (java.io.*)
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
 
Jsp/Servlet
Jsp/ServletJsp/Servlet
Jsp/Servlet
 
Java Basics
Java BasicsJava Basics
Java Basics
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class 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
 

Similar to Byte stream classes.49

Byte arrayinputstream.50
Byte arrayinputstream.50Byte arrayinputstream.50
Byte arrayinputstream.50myrajendra
 
L21 io streams
L21 io streamsL21 io streams
L21 io streamsteach4uin
 
Input output files in java
Input output files in javaInput output files in java
Input output files in javaKavitha713564
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/ONem Sothea
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47myrajendra
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptxRathanMB
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfVithalReddy3
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.ioNilaNila16
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxDrYogeshDeshmukh1
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In JavaRajan Shah
 
H U F F M A N Algorithm Class
H U F F M A N Algorithm ClassH U F F M A N Algorithm Class
H U F F M A N Algorithm ClassJade Danial
 
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
 
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxChapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxnoonoboom
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output ConceptsVicter Paul
 

Similar to Byte stream classes.49 (20)

Byte arrayinputstream.50
Byte arrayinputstream.50Byte arrayinputstream.50
Byte arrayinputstream.50
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Input output files in java
Input output files in javaInput output files in java
Input output files in java
 
Java Day-6
Java Day-6Java Day-6
Java Day-6
 
Java program file I/O
Java program file I/OJava program file I/O
Java program file I/O
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
 
Computer science input and output BASICS.pptx
Computer science input and output BASICS.pptxComputer science input and output BASICS.pptx
Computer science input and output BASICS.pptx
 
CSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdfCSE3146-ADV JAVA M2.pdf
CSE3146-ADV JAVA M2.pdf
 
Java I/O
Java I/OJava I/O
Java I/O
 
Input/Output Exploring java.io
Input/Output Exploring java.ioInput/Output Exploring java.io
Input/Output Exploring java.io
 
IOStream.pptx
IOStream.pptxIOStream.pptx
IOStream.pptx
 
Unit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptxUnit No 5 Files and Database Connectivity.pptx
Unit No 5 Files and Database Connectivity.pptx
 
Files and streams In Java
Files and streams In JavaFiles and streams In Java
Files and streams In Java
 
CORE JAVA-1
CORE JAVA-1CORE JAVA-1
CORE JAVA-1
 
Input & output
Input & outputInput & output
Input & output
 
H U F F M A N Algorithm Class
H U F F M A N Algorithm ClassH U F F M A N Algorithm Class
H U F F M A N Algorithm Class
 
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
 
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptxChapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
Chapter 13_m5JAVANOTESAPPLETS,INPUT.pptx
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
Io stream
Io streamIo stream
Io stream
 

More from myrajendra (20)

Fundamentals
FundamentalsFundamentals
Fundamentals
 
Data type
Data typeData type
Data type
 
Hibernate example1
Hibernate example1Hibernate example1
Hibernate example1
 
Jdbc workflow
Jdbc workflowJdbc workflow
Jdbc workflow
 
2 jdbc drivers
2 jdbc drivers2 jdbc drivers
2 jdbc drivers
 
3 jdbc api
3 jdbc api3 jdbc api
3 jdbc api
 
4 jdbc step1
4 jdbc step14 jdbc step1
4 jdbc step1
 
Dao example
Dao exampleDao example
Dao example
 
Sessionex1
Sessionex1Sessionex1
Sessionex1
 
Internal
InternalInternal
Internal
 
3. elements
3. elements3. elements
3. elements
 
2. attributes
2. attributes2. attributes
2. attributes
 
1 introduction to html
1 introduction to html1 introduction to html
1 introduction to html
 
Headings
HeadingsHeadings
Headings
 
Forms
FormsForms
Forms
 
Css
CssCss
Css
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Views
ViewsViews
Views
 
Starting jdbc
Starting jdbcStarting jdbc
Starting jdbc
 

Byte stream classes.49

  • 1. ByteStream classes http://improvejava.blogspot.in/ 1
  • 2. Objective On completion of this period, you would be able to learn • ByteStream classes http://improvejava.blogspot.in/ 2
  • 3. Recap In the last class, you have studied about the File class • You have also studied about the various methods, and properties of a File class http://improvejava.blogspot.in/ 3
  • 4. Types of Stream Class Java’s stream-based I/O is built upon four abstract classes: 1. Input Stream 2. Output Stream 3. Reader 4. Writer http://improvejava.blogspot.in/ 4
  • 5. Byte Streams Byte Streams are two types 1. InputStream class 2. OutputStream class http://improvejava.blogspot.in/ 5
  • 6. InputStream class • Data is accessed as a sequence of bytes • Its an abstract class that defines streaming byte input • The methods in the input stream are used to read bytes from input stream • All methods in this class will throw an I/O Exception http://improvejava.blogspot.in/ 6
  • 7. OutputStream class • Is an abstract class that defines streaming byte output • The methods of it are used to send bytes to the output stream • All methods in this class return a void value and throw an I/O Exception in the case of errors http://improvejava.blogspot.in/ 7
  • 8. FileInputStream class • Is a subclass of InputStream that you can use to read bytes from a file • Its has two constructors • FileInputStream(String filePath) • FileInputStream(File fileObj) • When FileInputStream is created, it is also opened for reading • FileInputStream overrides six of the methods in the abstract class InputStream http://improvejava.blogspot.in/ 8
  • 9. // Program for FileInputStream import java.io.*; class FileInputStreamDemo { public static void main(String args[]) throws Exception { int size; InputStream f = new FileInputStream("FileInputStreamDemo.java"); System.out.println("Total Available Bytes: " + (size = f.available())); int n = size/40; System.out.println("First " + n + " bytes of the file one read() at a time"); for (int i=0; i < n; i++) { System.out.print((char) f.read()); } System.out.println("nStill Available: " + f.available()); System.out.println("Reading the next " + n + " with one read(b[])"); http://improvejava.blogspot.in/ 9
  • 10. // Program for FileInputStream byte b[] = new byte[n]; if (f.read(b) != n) { System.err.println("couldn't read " + n + " bytes."); } System.out.println(new String(b, 0, n)); System.out.println("nStill Available: " + (size = f.available())); System.out.println("Skipping half of remaining bytes with skip()"); f.skip(size/2); System.out.println("Still Available: " + f.available()); System.out.println("Reading " + n/2 + " into the end of array"); if (f.read(b, n/2, n/2) != n/2) { System.err.println("couldn't read " + n/2 + " bytes."); } System.out.println(new String(b, 0, b.length)); System.out.println("nStill Available: " + f.available()); f.close(); } } http://improvejava.blogspot.in/ 10
  • 11. FileOutputStream class • Is a subclass of OutputStream that you can use to write bytes to a file • Constructors are • FileOutputStream(String filePath) • FileOutputStream(File fileObj) • FileOutputStream(String filePath, boolean append) • FileOutputStream(File fileObj, boolean append) • They can throw a FileNotFoundException or a SecurityException http://improvejava.blogspot.in/ 11
  • 12. Summary • Byte Streams are two types: InputStream and OutputStream • In InputStream data is accessed as a sequence of bytes • FileInputStream and FilOuputStream are the subclasses of ByteStream http://improvejava.blogspot.in/ 12
  • 13. Quiz 1. All methods in InputStream class will throw an IOException A. True B. False http://improvejava.blogspot.in/ 13
  • 14. Quiz contd .. 2. Which method is used to read bytes from InpuStream A. readLine() B. read() C. write() D. None http://improvejava.blogspot.in/ 14
  • 15. Frequently Asked Questions 1. Write about Byte Stream classes 2. Write a java program which illustrates the use of FileInputStream http://improvejava.blogspot.in/ 15