जावा प्रोग्राम एक स्ट्रिंग को इनपुटस्ट्रीम में कन्वर्ट करने के लिए

इस कार्यक्रम में, हम एक स्ट्रिंग को जावा में एक इनपुटस्ट्रीम में बदलना सीखेंगे।

इस उदाहरण को समझने के लिए, आपको निम्नलिखित जावा प्रोग्रामिंग विषयों का ज्ञान होना चाहिए:

  • जावा स्ट्रिंग
  • जावा इनपुटस्ट्रीम क्लास
  • जावा बाइटएयरएयरप्यूटस्ट्रीम क्लास

उदाहरण: स्ट्रिंग को इनपुटस्ट्रीम में बदलने के लिए जावा प्रोग्राम

 import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class Main ( public static void main(String args()) ( // Creates a string String name = "Programiz"; System.out.println("String is: " + name); try ( InputStream stream = new ByteArrayInputStream(name.getBytes(StandardCharsets.UTF_8)); System.out.println("InputStream: " + stream); // Returns the available number of bytes System.out.println("Available bytes at the beginning: " + stream.available()); // Reads 3 bytes from the stream stream stream.read(); stream.read(); stream.read(); // After reading 3 bytes // Returns the available number of bytes System.out.println("Available bytes at the end: " + stream.available()); stream.close(); ) catch (Exception e) ( e.getStackTrace(); ) ) )

आउटपुट

 स्ट्रिंग है: Programiz InputStream: java.io.ByteArrayInputStream@5479e3f शुरुआत में उपलब्ध बाइट्स: 9 उपलब्ध बाइट्स अंत में: 6

उपरोक्त उदाहरण में, हमने एक स्ट्रिंग नाम बनाया है। यहां, हम स्ट्रिंग को इनपुट स्ट्रीम नाम स्ट्रीम में परिवर्तित कर रहे हैं।

getBytes()विधि बाइट्स में स्ट्रिंग बदल देता है। अधिक जानने के लिए, जावा स्ट्रिंग getBytes पर जाएँ ()

दिलचस्प लेख...