Java FileInputStream Class
Java FileInputStream Class
The main purpose of the Java FileInputStream class is to read streams of raw data from the file in the file system. The FileInputStream is a subclass of InputStream. InputStream is an abstract superclass.
public class FileInputStream extends InputStream
{
…
}
Constructors
The most used constructors of the class are as follows:
public FileInputStream(String name)
public FileInputStream(File file)
Methods
The important method is the read() method, which reads a byte of data from the input stream.
To improve performance, the read( byte[]) is used for reading the byte array. The byte array is read into a temporary buffer area in the main memory.
Example
FileInputStream fis = new FileInputStream(“input.txt”);
or
File file = new File(“input.txt”);
FileInputStream fis = new FileInputStream(file);
Sample Code
package com.testingdocs.streams; import java.io.FileInputStream; import java.io.IOException; /* * Program to demo FileInputStream class * Java Tutorials - www.TestingDocs.com */ public class FileReadDemo { public static void main(String[] args) throws IOException { FileInputStream fis = null; int b = 0; try{ // create a file input stream object fis = new FileInputStream("input.txt"); while((b = fis.read()) != -1 ) System.out.println((char) b); // type cast }catch(Exception e) { e.printStackTrace(); } finally { fis.close(); } } }
—
Java Tutorials
Java Tutorial on this website:
https://www.testingdocs.com/java-tutorial/