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.
| Type | Purpose | Example |
|---|---|---|
| Input (Reading) | Read data from file to memory | FileInputStream, FileReader |
| Output (Writing) | Write data from memory to file | FileOutputStream, FileWriter |
| Byte-Based | Binary data processing | InputStream, OutputStream |
| Character-Based | Text data processing | Reader, 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 backupsFile I/O से applications disk पर data को read/write कर सकते हैं।
Was this answer clear?