0% found this document useful (0 votes)
30 views19 pages

Lecture-2.1.2

This document discusses Java FileInputStream Class, Java ByteArrayOutputStream, and Java ByteArrayInputStream. It provides examples of reading and writing files and byte arrays using these classes. Methods of each class are described, including reading and writing bytes, characters, and converting between byte arrays and strings. The document ends with references for further reading.

Uploaded by

gdgd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views19 pages

Lecture-2.1.2

This document discusses Java FileInputStream Class, Java ByteArrayOutputStream, and Java ByteArrayInputStream. It provides examples of reading and writing files and byte arrays using these classes. Methods of each class are described, including reading and writing bytes, characters, and converting between byte arrays and strings. The document ends with references for further reading.

Uploaded by

gdgd
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

INSTITUTE : UIE

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

Java FileInputStream Class,


Java ByteArrayOutputStream, Java ByteArrayInputStream

DISCOVER . LEARN . EMPOWER


Lecture Objectives

In this lecture, we will discuss:


• Java FileInputStream Class
• Java ByteArrayOutputStream
• Java ByteArrayInputStream

2
Java FileInputStream Class
The FileInputStream class creates an InputStream that you can use to read bytes from a file.

FileInputStream(String filepath)
FileInputStream(File fileObj) throw a FileNotFoundException

Eg::

FileInputStream f0 = new FileInputStream("/autoexec.bat")


File f = new File("/autoexec.bat");
FileInputStream f1 = new FileInputStream(f);

FileInputStream overrides six of the methods in the abstract class InputStream.

The mark( ) and reset( ) methods are not overridden, and any attempt to use reset( ) on a
FileInputStream will generate an IOException.
Java FileInputStream class methods
Java FileInputStream example 1: read single character
import java.io.FileInputStream;  
public class DataStreamExample {  
     public static void main(String args[]){    
          try{    FileInputStream fin=new FileInputStream("D:\\testout.txt");    
            int i=fin.read();  
            System.out.print((char)i);    
            fin.close();    
          }catch(Exception e){System.out.println(e);}    
         }    
        }  
Note: Before running the code, a text file named "testout.txt" is required to be
created. In this file, we are having the following content:
Welcome to javatpoint.

After executing the above program, you will get a single character from the file
which is 87 (in byte form). To see the text, you need to convert it into character.

Output:
W
Java FileInputStream example 2: read all characters
import java.io.FileInputStream;  
public class DataStreamExample {  
     public static void main(String args[]){    
          try{    
            FileInputStream fin=new FileInputStream("D:\\testout.txt");    
            int i=0;    
            while((i=fin.read())!=-1){    
             System.out.print((char)i);    
            }    
            fin.close();    
          }catch(Exception e){
System.out.println(e);
}    
         }    
        }  
Output:
Welcome to javaTpoint
QUIZ:

Which of these class contains the methods used to write in a file?


a) FileStream
b) FileInputStream
c) Fileinputstream

Which of these exception is thrown in cases when the file specified for
writing is not found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
ByteArrayOutputStream

ByteArrayOutputStream is an implementation of an output stream that uses a byte


array as the destination.

ByteArrayOutputStream( ) // a buffer of 32 bytes is created


ByteArrayOutputStream(int numBytes)
Java ByteArrayOutputStream class methods
Example
import java.io.*;
class ByteArrayOutputStreamDemo {
public static void main(String args[]) throws IOException {
ByteArrayOutputStream f = new ByteArrayOutputStream();

String s = "This should end up in the array";


byte buf[] = s.getBytes();
f.write(buf);

System.out.println("Buffer as a string");
System.out.println(f.toString());

System.out.println("Into array");
byte b[] = f.toByteArray();
for (int i=0; i<b.length; i++) {
System.out.print((char) b[i]);
}
System.out.println("\nTo an OutputStream()");
OutputStream f2 = new FileOutputStream("test.txt");
f.writeTo(f2);
f2.close();

System.out.println("Doing a reset");
f.reset();
for (int i=0; i<3; i++)
f.write('X');
System.out.println(f.toString());

System.out.println("\nTo an OutputStream()");
OutputStream f3 = new FileOutputStream("test1.txt");
f.writeTo(f3);
f3.close();
Additional:: this example uses the writeTo( ) convenience
} method to write the contents of f to test.txt.
}
ByteArrayInputStream

ByteArrayInputStream is an implementation of an input stream that uses a byte


array as the source.

ByteArrayInputStream(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int numBytes)

Java ByteArrayInputStream class contains an internal buffer which is used to read


byte array as stream. In this stream, the data is read from a byte array.

The buffer of ByteArrayInputStream automatically grows according to data.


Java ByteArrayInputStream class methods
Example
import java.io.*;
class ByteArrayInputStreamDemo {
public static void main(String args[]) throws IOException { String tmp =
"abcdefghijklmnopqrstuvwxyz";
byte b[] = tmp.getBytes();
ByteArrayInputStream input1 = new ByteArrayInputStream(b);
ByteArrayInputStream input2 = new ByteArrayInputStream(b,0,13);
int c;

while ((c = input1.read()) != -1) {


System.out.print((char) c); }
System.out.println(""); The input1 object contains the complete byte array, while
System.out.println("Second"); input2 contains only the
first thirteen letters.
while ((c = input2.read()) != -1) {
System.out.print((char) c); } } }
QUIZ:

What is the return type of the available method?


a) int
b) Float

Which exception is thrown by the read( ) method of InputStream class.


A) Exception
B) IOException
C) ReadException
D) File Not Found Exception
Summary:

In this session, you were able to :

•Learn about Java FileInputStream Class.


•Learn about Java ByteArrayOutputStream,
Java ByteArrayInputStream
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/fnFQWtZZE-4
https://youtu.be/5s1FQMoWuJs

Reference Links:
https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayInputStream.html
https://docs.oracle.com/javase/7/docs/api/java/io/ByteArrayOutputStream.html
https://www.javatpoint.com/java-fileinputstream-class
https://www.javatpoint.com/java-bytearrayoutputstream-class
https://www.javatpoint.com/java-bytearrayinputstream-class
THANK YOU

You might also like