Java
 Computer >> コンピューター >  >> プログラミング >> Java

文字列をInputStreamに変換するJavaプログラム


この記事では、文字列をinputStreamに変換する方法を理解します。文字列は、1つ以上の文字を含み、二重引用符(“”)で囲まれたデータ型です。 InputStreamクラスは、バイトの入力ストリームを表すすべてのクラスのスーパークラスです。

以下は同じのデモンストレーションです-

入力がであると仮定します −

Input string: Java Program

必要な出力は

The number of bytes available at the beginning: 12
The number of bytes available at the end: 10

アルゴリズム

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

例1

ここでは、「main」関数の下ですべての操作をバインドします。

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

出力

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

例2

ここでは、操作をオブジェクト指向プログラミングを示す関数にカプセル化します。

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

出力

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

  1. Javaで文字列をInputStreamオブジェクトに変換する方法は?

    ByteArrayInputStream InputStreamのサブクラスです クラスであり、バイトを含む内部バッファが含まれています ストリームから読み取ることができます。 文字列をInputStreamに変換できます ByteArrayInputStreamを使用したオブジェクト クラス。このクラスコンストラクタは、 getBytes()を呼び出すことで実行できる文字列バイト配列を取ります。 Stringクラスのメソッド。 例 import java.io.*; public class StringToInputStreamTest {    public s

  2. 文字のリストを文字列に変換するPythonプログラム

    Pythonはこの種の変換をたくさん必要とします。たとえば、このような変換はシリアル化の目的で役立ちます。このような変換の例は、-です。 ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'] to "hello world" Pythonには、このような変換に使用できる結合メソッドがあります。オブジェクトを連結するために使用される区切り文字列