Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 5 of 10 · File Handling and I/O Operations
Interview question

What are FileInputStream and FileOutputStream for binary files? Binary files के लिए FileInputStream और FileOutputStream क्या हैं?

Answer

FileInputStream reads binary data from files, while FileOutputStream writes binary data to files. They work with bytes instead of characters, making them suitable for images, videos, and binary files.

import java.io.*;

public class BinaryFileDemo {
    public static void main(String[] args) {
        // Writing binary data
        try (FileOutputStream fos = new FileOutputStream('binary.bin')) {
            byte[] data = {65, 66, 67, 68, 69};  // ABCDE
            fos.write(data);  // Write entire array
            fos.write(70);    // Write single byte
            System.out.println('Binary file written');
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // Reading binary data
        try (FileInputStream fis = new FileInputStream('binary.bin')) {
            int byte_value;
            while ((byte_value = fis.read()) != -1) {
                System.out.print((char) byte_value + ' ');
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

// Copy binary file (image, video, etc.)
public class FileCopyDemo {
    public static void copyFile(String source, String destination) {
        try (FileInputStream fis = new FileInputStream(source);
             FileOutputStream fos = new FileOutputStream(destination)) {
            
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }
            System.out.println('File copied successfully');
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        copyFile('source.jpg', 'destination.jpg');
    }
}

// FileInputStream methods
FileInputStream fis = new FileInputStream('file.bin');
int byte_val = fis.read();           // Read single byte (-1 if EOF)
byte[] buffer = new byte[1024];
int bytesRead = fis.read(buffer);    // Read into buffer
long skipped = fis.skip(100);        // Skip bytes
fis.close();

// FileOutputStream methods
FileOutputStream fos = new FileOutputStream('file.bin');
fos.write(65);                       // Write single byte
fos.write(new byte[]{65, 66, 67});   // Write array
fos.flush();                         // Flush to disk
fos.close();
Binary File I/O:

FileOutputStream (Writing):
try (FileOutputStream fos = new FileOutputStream('file.bin')) {
    fos.write(byteArray);
    fos.flush();
}

FileInputStream (Reading):
try (FileInputStream fis = new FileInputStream('file.bin')) {
    byte[] buffer = new byte[1024];
    int bytesRead = fis.read(buffer);
}

Use Case: Images, videos, executables
Adaptive Buffering: Use byte arrays for efficiency

Was this answer clear?