Lecture-2.1.2
Lecture-2.1.2
DEPARTMENT : CSE
Bachelor of Engineering (Computer Science & Engineering)
Java Programing(CST-205)
TOPIC OF PRESENTATION:
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::
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 exception is thrown in cases when the file specified for
writing is not found?
a) IOException
b) FileException
c) FileNotFoundException
d) FileInputException
ByteArrayOutputStream
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(byte array[ ])
ByteArrayInputStream(byte array[ ], int start, int numBytes)
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