The FileInputStream class in Java is used to read data from a file in the form of bytes. It’s ideal for reading binary data such as images or audio files. For reading text files, it’s better to use FileReader.
Direct Access: It directly reads the file content from the disk without buffering
Platform Independent: It can work on any operating system
Declaration
The FileInputStream class extends the InputStream class, which means it inherits methods for reading raw byte data from files.
public class FileInputStream extends InputStream
Example: FileInputStream class to read data from file.
Java
importjava.io.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Use try-with-resources to automatically close the// streamtry(FileInputStreamfi=newFileInputStream("file1.txt")){// Display file channel informationSystem.out.println("Channel: "+fi.getChannel());// Display file descriptorSystem.out.println("File Descriptor: "+fi.getFD());// Show available bytes in the streamSystem.out.println("Number of remaining bytes: "+fi.available());// Skip first few bytesfi.skip(4);System.out.println("File Contents:");// Read and print file contentintch;while((ch=fi.read())!=-1){System.out.print((char)ch);}}catch(FileNotFoundExceptione){System.out.println("File not found: Ensure 'file1.txt' exists in the working directory.");}catch(IOExceptione){System.out.println("An error occurred while reading the file: "+e.getMessage());}}}
Output:
Output
Constructors ofFileInputStream Class
1. FileInputStream(String name)
Creates an input file stream to read from a file with the specified name.
FileInputStream fi = new FileInputStream("example.txt");
2. FileInputStream(File file)
Creates an input file stream to read from the specified File object.
File f = new File("example.txt"); FileInputStream fi = new FileInputStream(f);
3. FileInputStream(FileDescriptor fdobj)
Creates an input file stream to read from the specified file descriptor.
FileDescriptor fd = FileDescriptor.in; FileInputStream fi = new FileInputStream(fd);
Read Data from a File Using FileInputStream
Create a file named file.txt in your project directory with the following content:
this is my first code this is my second code
Java
importjava.io.*;publicclassGeeks{publicstaticvoidmain(String[]args){// Use try-with-resources to automatically close the streamtry(FileInputStreamfi=newFileInputStream("file1.txt")){// Display file channel informationSystem.out.println("Channel: "+fi.getChannel());// Display file descriptorSystem.out.println("File Descriptor: "+fi.getFD());// Illustrating available methodSystem.out.println("Number of remaining bytes: "+fi.available());// Illustrating skip() methodfi.skip(4);System.out.println("File Contents:");// Reading characters from FileInputStreamintch;while((ch=fi.read())!=-1){System.out.print((char)ch);}}catch(FileNotFoundExceptione){System.out.println("File not found: Ensure 'file1.txt' exists in the working directory.");}catch(IOExceptione){System.out.println("An error occurred while reading the file: "+e.getMessage());}}}
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.