स्ट्रीम चेनिंग
आपकी कंपनी के पास विभिन्न टेक्स्ट एनकोडिंग में डेटा फाइलें हैं. प्रोसेसिंग को मानकीकृत करने के लिए, आपके मैनेजर ने आपसे ऐसा फाइल रीडर बनाने को कहा है जो UTF-8 एनकोडिंग को स्पष्ट रूप से हैंडल करे. इसके लिए कई स्ट्रीम्स को एक साथ चेन करना होगा—जो Java I/O का एक महत्वपूर्ण कॉन्सेप्ट है.
BufferedReader, InputStreamReader, FileInputStream, और StandardCharsets क्लासेस आपके लिए इम्पोर्ट कर दी गई हैं.
यह अभ्यास पाठ्यक्रम का हिस्सा है
Java में डेटा इम्पोर्ट करना
अभ्यास निर्देश
BufferedReaderकोInputStreamReaderऔर उसेFileInputStreamसे चेन करें.- फाइल से पहली लाइन पढ़कर प्रिंट करें.
इंटरैक्टिव व्यावहारिक अभ्यास
इस अभ्यास को इस नमूना कोड को पूरा करके आज़माएँ।
public class StreamChainingExample {
public static void main(String[] args) {
String filePath = "sample_market_data.csv";
// Chain three stream classes together
try (
BufferedReader reader = new ____(
new ____(
new ____(filePath),
StandardCharsets.UTF_8
)
)
) {
// Read and print the first line
String firstLine = reader.____();
System.out.println("First line (UTF-8): " + ____);
System.out.println("Successfully read using chained streams");
} catch (IOException e) {
System.err.println("Error: " + e.getMessage());
}
}
}