Interview question
What is Character Encoding in file I/O? How do you handle different charsets? File I/O में Character Encoding क्या है? Different charsets कैसे handle करते हैं?
Answer
Character encoding specifies how characters are represented as bytes. Different encodings (UTF-8, UTF-16, ASCII, ISO-8859-1) store characters differently. Incorrect encoding causes garbled text.
import java.io.*;
import java.nio.charset.StandardCharsets;
public class CharsetDemo {
public static void main(String[] args) {
String text = 'Hello, 你好, नमस्ते';
// Writing with specific charset
try (FileWriter fw = new FileWriter('utf8.txt',
StandardCharsets.UTF_8)) {
fw.write(text);
System.out.println('UTF-8 file written');
} catch (IOException e) {
e.printStackTrace();
}
// Reading with specific charset
try (FileReader fr = new FileReader('utf8.txt',
StandardCharsets.UTF_8)) {
int ch;
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
} catch (IOException e) {
e.printStackTrace();
}
// Using InputStreamReader for charset control
try (InputStreamReader isr = new InputStreamReader(
new FileInputStream('file.txt'),
StandardCharsets.UTF_8)) {
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
// Using OutputStreamWriter for charset control
try (OutputStreamWriter osw = new OutputStreamWriter(
new FileOutputStream('output.txt'),
StandardCharsets.UTF_8)) {
osw.write('Multilingual text: Hello, 世界, мир');
} catch (IOException e) {
e.printStackTrace();
}
}
}
// Common charsets
// UTF-8: Variable-length encoding (1-4 bytes per character)
// UTF-16: Fixed 2-byte encoding
// ASCII: 7-bit encoding (English only)
// ISO-8859-1: European characters
// GBK: Chinese characters
// Detect encoding
public class EncodingDetection {
public static String detectEncoding(File file) {
// Using Apache Commons IO or ICU4J libraries
// Or detect BOM (Byte Order Mark)
return 'UTF-8';
}
}Character Encoding:
1. UTF-8 (Most common):
- Variable-length (1-4 bytes)
- Supports all languages
- Backward-compatible with ASCII
2. UTF-16: Fixed 2-byte
3. ASCII: English only (7-bit)
4. ISO-8859-1: European chars
Handling Different Charsets:
FileWriter/FileReader (Java 11+):
try (FileWriter fw = new FileWriter('file.txt',
StandardCharsets.UTF_8)) {
fw.write(text);
}
InputStreamReader (older Java):
try (InputStreamReader isr = new InputStreamReader(
new FileInputStream('file.txt'),
StandardCharsets.UTF_8)) {
// Read with specific charset
}Was this answer clear?