SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

Tendances

Applet life cycle
Applet life cycleApplet life cycle
Applet life cyclemyrajendra
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiersNilimesh Halder
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++HalaiHansaika
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in javaAtul Sehdev
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++Jayant Dalvi
 
Type casting in java
Type casting in javaType casting in java
Type casting in javaFarooq Baloch
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statementRaj Parekh
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaRaghu nath
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVAVINOTH R
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in clavanya marichamy
 
File Handling Python
File Handling PythonFile Handling Python
File Handling PythonAkhil Kaushik
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Java Data Types and Variables
Java Data Types and VariablesJava Data Types and Variables
Java Data Types and Variablessasi saseenthiran
 

Tendances (20)

Packages in java
Packages in javaPackages in java
Packages in java
 
Java threads
Java threadsJava threads
Java threads
 
Java constructors
Java constructorsJava constructors
Java constructors
 
Applet life cycle
Applet life cycleApplet life cycle
Applet life cycle
 
Lesson 02 python keywords and identifiers
Lesson 02   python keywords and identifiersLesson 02   python keywords and identifiers
Lesson 02 python keywords and identifiers
 
classes and objects in C++
classes and objects in C++classes and objects in C++
classes and objects in C++
 
itft-Decision making and branching in java
itft-Decision making and branching in javaitft-Decision making and branching in java
itft-Decision making and branching in java
 
Arrays and Strings
Arrays and Strings Arrays and Strings
Arrays and Strings
 
Exception handling c++
Exception handling c++Exception handling c++
Exception handling c++
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
 
Switch statement, break statement, go to statement
Switch statement, break statement, go to statementSwitch statement, break statement, go to statement
Switch statement, break statement, go to statement
 
Exception handling
Exception handlingException handling
Exception handling
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Dynamic memory allocation in c
Dynamic memory allocation in cDynamic memory allocation in c
Dynamic memory allocation in c
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
File Handling Python
File Handling PythonFile Handling Python
File Handling Python
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Java program structure
Java program structureJava program structure
Java program structure
 
Java Data Types and Variables
Java Data Types and VariablesJava Data Types and Variables
Java Data Types and Variables
 

En vedette

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
 

En vedette (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
 

Similaire à 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
 

Similaire à 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
 

Plus 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