SlideShare uma empresa Scribd logo
1 de 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

Mais conteúdo relacionado

Mais procurados

Java Data Types
Java Data TypesJava Data Types
Java Data TypesSpotle.ai
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packagesVINOTH R
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-threadjavaicon
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++Shyam Gupta
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAsivasundari6
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programmingAmar Jukuntla
 
Presentation Routing algorithm
Presentation Routing algorithmPresentation Routing algorithm
Presentation Routing algorithmBasit Hussain
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
User datagram protocol (udp)
User datagram protocol (udp)User datagram protocol (udp)
User datagram protocol (udp)Ramola Dhande
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handlingkamal kotecha
 
Control Unit Design
Control Unit DesignControl Unit Design
Control Unit DesignVinit Raut
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference ModelMukesh Tekwani
 

Mais procurados (20)

Java Data Types
Java Data TypesJava Data Types
Java Data Types
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
1.Role lexical Analyzer
1.Role lexical Analyzer1.Role lexical Analyzer
1.Role lexical Analyzer
 
Evolution of os
Evolution of osEvolution of os
Evolution of os
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Stream classes in C++
Stream classes in C++Stream classes in C++
Stream classes in C++
 
WHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVAWHAT IS ABSTRACTION IN JAVA
WHAT IS ABSTRACTION IN JAVA
 
Need of object oriented programming
Need of object oriented programmingNeed of object oriented programming
Need of object oriented programming
 
Presentation Routing algorithm
Presentation Routing algorithmPresentation Routing algorithm
Presentation Routing algorithm
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Java Streams
Java StreamsJava Streams
Java Streams
 
Data types
Data typesData types
Data types
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
User datagram protocol (udp)
User datagram protocol (udp)User datagram protocol (udp)
User datagram protocol (udp)
 
Applets
AppletsApplets
Applets
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Control Unit Design
Control Unit DesignControl Unit Design
Control Unit Design
 
TCP-IP Reference Model
TCP-IP Reference ModelTCP-IP Reference Model
TCP-IP Reference Model
 

Destaque

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
 

Destaque (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
 
Files in java
Files in javaFiles in java
Files in java
 
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
 

Semelhante a Byte stream classes.49

Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51myrajendra
 
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
 

Semelhante a Byte stream classes.49 (20)

Character stream classes introd .51
Character stream classes introd  .51Character stream classes introd  .51
Character stream classes introd .51
 
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
 

Mais de 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