Java FileInputStream
Reading data from files using byte streams
📖 What is FileInputStream?
FileInputStream is a Java class used to read raw bytes from files. It's perfect for reading binary data, images, or any file content byte by byte efficiently.
// Basic FileInputStream example
import java.io.*;
FileInputStream fis = new FileInputStream("data.txt");
int data = fis.read();
fis.close();
Result:
Reads first byte from 'data.txt'
FileInputStream Features
File Reading
Read bytes from any file
FileInputStream fis = new FileInputStream("file.txt");
Byte Operations
Read data byte by byte
int byteData = fis.read();
Binary Data
Handle images, videos, etc.
byte[] buffer = new byte[1024];
Performance
Efficient for large files
fis.read(buffer, 0, 1024);
🔹 Basic FileInputStream Usage
Here's how to read a file using FileInputStream:
import java.io.*;
public class FileInputExample {
public static void main(String[] args) {
try {
// Create FileInputStream
FileInputStream fis = new FileInputStream("sample.txt");
// Read single byte
int data = fis.read();
while (data != -1) {
System.out.print((char) data);
data = fis.read();
}
// Close the stream
fis.close();
} catch (IOException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output:
Hello World from file!
🔹 Reading with Byte Array
More efficient way to read larger files:
import java.io.*;
public class ByteArrayRead {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("document.txt");
// Create byte array buffer
byte[] buffer = new byte[1024];
int bytesRead = fis.read(buffer);
while (bytesRead != -1) {
// Convert bytes to string
String content = new String(buffer, 0, bytesRead);
System.out.print(content);
bytesRead = fis.read(buffer);
}
fis.close();
} catch (IOException e) {
System.out.println("File error: " + e.getMessage());
}
}
}
Output:
This is the content of document.txt file read in chunks!
🔹 Try-with-Resources
Modern way to handle FileInputStream with automatic resource management:
import java.io.*;
public class ModernFileRead {
public static void main(String[] args) {
// Try-with-resources automatically closes the stream
try (FileInputStream fis = new FileInputStream("data.txt")) {
byte[] buffer = new byte[512];
int bytesRead = fis.read(buffer);
if (bytesRead > 0) {
String content = new String(buffer, 0, bytesRead);
System.out.println("File content: " + content);
}
} catch (FileNotFoundException e) {
System.out.println("File not found!");
} catch (IOException e) {
System.out.println("Reading error: " + e.getMessage());
}
// Stream is automatically closed here
}
}
Output:
File content: Java FileInputStream is awesome!
🔹 Common Methods
Important FileInputStream methods:
- read() - Reads single byte, returns -1 at end
- read(byte[] b) - Reads bytes into array
- read(byte[] b, int off, int len) - Reads with offset
- available() - Returns available bytes to read
- skip(long n) - Skips n bytes
- close() - Closes the stream
// Method examples
FileInputStream fis = new FileInputStream("test.txt");
int available = fis.available(); // Check available bytes
fis.skip(10); // Skip first 10 bytes
int data = fis.read(); // Read one byte
fis.close(); // Always close!