You are on page 1of 18

INSTITUTE : UIE

DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:

Introduction to Streams: Java FileOutputStream Class

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• Introduction to Streams
• Java FileOutputStream Class

2
Introduction to Streams
• Java programs perform I/O through streams. A stream is:
– an abstraction that either produces or consumes information
– linked to a physical device by the Java I/O system

• All streams behave similarly, even if the actual physical devices to which they are
linked differ.

• Thus the same I/O classes can be applied to any kind of device as they abstract the
difference between different I/O devices.
• Java’s stream classes are defined in the java.io package.
• Java 2 defines two types of streams:
– byte streams
– character streams
• Byte streams:
– provide a convenient means for handling input and output of bytes
– are used for reading or writing binary data
• Character streams:
– provide a convenient means for handling input and output of characters
– use Unicode, and, therefore, can be internationalized
Predefined Streams

• System class of the java.lang package contains three predefined stream


variables, in, out, and err.
• These variables are declared as public and static within System:
– System.out refers to the standard output stream which is the
console.
– System.in refers to standard input, which is the keyboard by
default.
– System.err refers to the standard error stream, which also is the
console by default.
Working of Java OutputStream and
InputStream
OutputStream Class
OutputStream class is an abstract class. It is the superclass of all classes representing an output
stream of bytes. An output stream accepts output bytes and sends them to some sink.

Useful methods of OutputStream


OutputStream Hierarchy
InputStream class
InputStream class is an abstract class. It is the superclass of all classes representing an input stream of bytes.

Useful methods of InputStream


InputStream Hierarchy
QUIZ:

Java input output classes are available in __________package


a. java.lang
b. java.io
c. java.util

What are the inbuilt streams available in java.io package


a. System.in
b. System.out
c. System.err
d. All of the above
QUIZ:

Match the streams with the appropriate phrases in column B

Column A Column B

FileInputStream Byte stream for reading from file

FileOutputStream Byte stream for writing to a file


Java FileOutputStream Class
Java FileOutputStream is an output stream used for writing data to a file
.

 used to write primitive values into a file.


 to write byte-oriented data.

FileOutputStream class declaration


public class FileOutputStream extends OutputStream  

13
FileOutputStream class methods

14
Example
Java FileOutputStream Example 1: write byte

import java.io.FileOutputStream;  
public class FileOutputStreamExample {  
    public static void main(String args[]){    
           try{    
             FileOutputStream fout=new FileOutputStream("D:\\testout.txt");    
Output:
             fout.write(65);    
Success...
             fout.close();    
The content of a text file testout.txt is set
             System.out.println("success...");    
with the data A.
            }catch(Exception e){System.out.println(e);}    
testout.txt
      }    
A
}
15
Summary:

In this session, you were able to :


• Learn about Introduction to Streams
• Understand Java FileOutputStream Class
References:
Books:
1. Balaguruswamy, Java.
2. A Primer, E.Balaguruswamy, Programming with Java, Tata McGraw Hill
Companies
3. John P. Flynt Thomson, Java Programming.

Video Lectures :
https://youtu.be/lYuj9nn0PBU

Reference Links:
https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
https://www.tutorialspoint.com/java/io/index.htm
https://www.tutorialspoint.com/java/io/java_io_fileoutputstream.htm
https://www.javatpoint.com/java-io
https://www.javatpoint.com/java-fileoutputstream-class
THANK YOU

You might also like