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

Tendances (20)

Core Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika TutorialsCore Java Tutorials by Mahika Tutorials
Core Java Tutorials by Mahika Tutorials
 
I/O Streams
I/O StreamsI/O Streams
I/O Streams
 
Java - Interfaces & Packages
Java - Interfaces & PackagesJava - Interfaces & Packages
Java - Interfaces & Packages
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Data types in java
Data types in javaData types in java
Data types in java
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Input output streams
Input output streamsInput output streams
Input output streams
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
Interface in java
Interface in javaInterface in java
Interface in java
 
Java Programming
Java ProgrammingJava Programming
Java Programming
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Applets in java
Applets in javaApplets in java
Applets in java
 
L21 io streams
L21 io streamsL21 io streams
L21 io streams
 
Reading and Writing Files
Reading and Writing FilesReading and Writing Files
Reading and Writing Files
 
Applets
AppletsApplets
Applets
 
Java - File Input Output Concepts
Java - File Input Output ConceptsJava - File Input Output Concepts
Java - File Input Output Concepts
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 

En vedette

Understanding java streams
Understanding java streamsUnderstanding java streams
Understanding java streams
Shahjahan Samoon
 
Character stream classes .52
Character stream classes .52Character stream classes .52
Character stream classes .52
myrajendra
 
Inner classes9 cm604.28
Inner classes9 cm604.28Inner classes9 cm604.28
Inner classes9 cm604.28
myrajendra
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
myrajendra
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
Ravi_Kant_Sahu
 
Object and Classes in Java
Object and Classes in JavaObject and Classes in Java
Object and Classes in Java
backdoor
 

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
 
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 Input Output and File Handling
Java Input Output and File HandlingJava Input Output and File Handling
Java Input Output and File Handling
 

Similaire à Byte stream classes.49

Byte arrayinputstream.50
Byte arrayinputstream.50Byte arrayinputstream.50
Byte arrayinputstream.50
myrajendra
 
Various io stream classes .47
Various io stream classes .47Various io stream classes .47
Various io stream classes .47
myrajendra
 
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
RathanMB
 
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
Jade 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 & Output
phanleson
 

Similaire à Byte stream classes.49 (20)

Byte arrayinputstream.50
Byte arrayinputstream.50Byte arrayinputstream.50
Byte arrayinputstream.50
 
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
 
Io stream
Io streamIo stream
Io stream
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output stream?
 
What is java input and output stream?
What is java input and output stream?What is java input and output stream?
What is java input and output 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