Subjects

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

What is File I/O in Java and why is it important? Java में File I/O क्या है और यह महत्वपूर्ण क्यों है?

Answer

File I/O (Input/Output) allows Java applications to read from and write to files on disk. It's essential for data persistence, log management, configuration handling, and inter-process communication.

TypePurposeExample
Input (Reading)Read data from file to memoryFileInputStream, FileReader
Output (Writing)Write data from memory to fileFileOutputStream, FileWriter
Byte-BasedBinary data processingInputStream, OutputStream
Character-BasedText data processingReader, Writer
// Basic File I/O Example
import java.io.*;

public class FileIODemo {
    public static void main(String[] args) {
        // Writing to file
        try (FileWriter fw = new FileWriter('data.txt')) {
            fw.write('Hello, File I/O!');
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        // Reading from file
        try (FileReader fr = new FileReader('data.txt')) {
            int character;
            while ((character = fr.read()) != -1) {
                System.out.print((char) character);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

// Benefits of File I/O:
// 1. Persistent storage - data survives application restart
// 2. Data exchange - share data between applications
// 3. Logging - record application events
// 4. Configuration - load app settings from files
// 5. Backup - create file backups

File I/O से applications disk पर data को read/write कर सकते हैं।

Was this answer clear?