
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Convert a String into the InputStream
In this article, we will understand how to convert a string into the inputStream. String is a datatype that contains one or more characters and is enclosed in double quotes(“ ”). InputStream class is the superclass of all classes representing an input stream of bytes.
Below is a demonstration of the same −
Suppose our input is −
Input string: Java Program
The desired output would be −
The number of bytes available at the beginning: 12 The number of bytes available at the end: 10
Algorithm
Step 1 - START Step 2 - Declare string namely input_string, an object of InputStream namely input_stream. Step 3 - Define the values. Step 4 - Use the function read() to read the bytes and .available() to fetch the available bytes. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class Demo { public static void main(String args[]) { String input_string = "Java Program"; System.out.println("The string is defined as: " + input_string); try { InputStream input_stream = new ByteArrayInputStream(input_string.getBytes(StandardCharsets.UTF_8)); System.out.println("The number of bytes available at the beginning: " + input_stream.available()); input_stream.read(); input_stream.read(); System.out.println("The number of bytes available at the end: " + input_stream.available()); input_stream.close(); } catch (Exception e) { e.getStackTrace(); } } }
Output
The string is defined as: Java Program The number of bytes available at the beginning: 12 The number of bytes available at the end: 10
Example 2
Here, we encapsulate the operations into functions exhibiting object-oriented programming.
import java.io.ByteArrayInputStream; import java.io.InputStream; import java.nio.charset.StandardCharsets; public class Demo { static void check_bytes(String input_string){ try { InputStream input_stream = new ByteArrayInputStream(input_string.getBytes(StandardCharsets.UTF_8)); System.out.println("The number of bytes available at the beginning: " + input_stream.available()); input_stream.read(); input_stream.read(); System.out.println("The number of bytes available at the end: " + input_stream.available()); input_stream.close(); } catch (Exception e) { e.getStackTrace(); } } public static void main(String args[]) { String input_string = "Java Program"; System.out.println("The string is defined as: " + input_string); check_bytes(input_string); } }
Output
The string is defined as: Java Program The number of bytes available at the beginning: 12 The number of bytes available at the end: 10
- Related Articles
- Java Program to Convert InputStream to String
- How to convert InputStream object to a String in Java?
- How to convert a String to an InputStream object in Java?\n
- convert InputStream to ByteArray in java
- Java program to convert a list of characters into a string
- Java Program to Convert the ArrayList into a string and vice versa
- How to convert InputStream to byte array in Java?
- Golang Program to convert a string into Uppercase
- Golang Program to convert a string into lowercase
- Java Program to convert a string into a numeric primitive type using Integer.valueOf()
- C++ Program to convert the string into a floatingpoint number
- How to convert a String into int in Java?
- Haskell Program to convert the string into an integer
- C++ Program to convert the string into an integer
- Golang program to convert the hash collection into string

Advertisements