FileInputStream in Java is a class that provides an InputStream for reading data from a file in a byte-oriented manner. It is a part of the java.io package and is commonly used for reading binary data, such as images, audio files, or any other file that contains raw bytes.
Here is the explanation of FileInputStream:
1. Purpose:
- FileInputStream is designed to read data from a file as a sequence of bytes. It is particularly useful when dealing with binary files where data is not meant to be interpreted as text.
2. Inheritance:
- FileInputStream extends the `InputStream` class, which is an abstract class for reading data from different sources. This makes FileInputStream suitable for reading bytes from a file.
3. Construction:
- To create an instance of FileInputStream, you typically pass the path of the file you want to read to its constructor. For example:
FileInputStream fis = new FileInputStream("path/to/file.txt");
4. Reading Bytes:
- FileInputStream provides various methods for reading bytes from the file, such as `read()`, `read(byte[] b)`, and `read(byte[] b, int off, int len)`. The `read()` method reads a single byte, while the other two methods read multiple bytes into a byte array.
5. Closing the Stream:
- It's essential to close the FileInputStream once you're done with it to release system resources. This is typically done using the `close()` method. Failing to close the stream may result in resource leaks.
6. Exceptions:
- Operations involving file I/O can throw checked exceptions, and FileInputStream is no exception. IOException is a common exception that needs to be handled when working with file streams. This ensures that your program can gracefully handle scenarios like file not found, permissions issues, or other I/O errors.
In summary, FileInputStream is a convenient class for reading raw binary data from a file in a byte-wise fashion, making it suitable for scenarios where you need to process non-textual information stored in files.
FileInputStream Example 1: Reading a File Byte by Byte
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample1 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
int byteRead;
while ((byteRead = fis.read()) != -1) {
System.out.print((char) byteRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream Example 2: Reading a File Block by Block
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample2 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("example.txt")) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
System.out.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream Example 3: Reading a File with BufferedInputStream
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample3 {
public static void main(String[] args) {
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("example.txt"))) {
int byteRead;
while ((byteRead = bis.read()) != -1) {
System.out.print((char) byteRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream Example 4: Reading Binary File
import java.io.FileInputStream;
import java.io.IOException;
public class FileInputStreamExample4 {
public static void main(String[] args) {
try (FileInputStream fis = new FileInputStream("binaryFile.bin")) {
int byteRead;
while ((byteRead = fis.read()) != -1) {
System.out.print(byteRead + " ");
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
FileInputStream Example 5: Reading File Using try-with-resources and Java NIO
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileInputStreamExample5 {
public static void main(String[] args) {
Path filePath = Path.of("example.txt");
try (FileInputStream fis = new FileInputStream(filePath.toFile())) {
int byteRead;
while ((byteRead = fis.read()) != -1) {
System.out.print((char) byteRead);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}