Write a Java Program to Convert a String into the InputStream

Java Program to Convert a String into the InputStream

Welcome to the World of Online Learning:

Hello Friends “This blog helps you to learn Java programming concepts. You can learn Java  language at your own speed and time. One can learn concepts of Java language by practicing various programs given on various pages of this blog. Enjoy the power of Self-learning using the Internet.”

Java Program to Convert a String into the InputStream
Java Program to Convert a String into the InputStream

Write a Java Program to Convert a String into the InputStream

PROGRAM: Java Program to Convert a String into the InputStream

/*Java Program to Convert a String into the InputStream*/

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();
}
}
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *