Computer >> Máy Tính >  >> Lập trình >> Java

Chương trình Java để chuyển đổi một chuỗi thành InputStream

Trong bài này, chúng ta sẽ hiểu cách chuyển một chuỗi thành inputStream. Chuỗi là một kiểu dữ liệu chứa một hoặc nhiều ký tự và được đặt trong dấu ngoặc kép (“”). Lớp InputStream là lớp cha của tất cả các lớp đại diện cho một luồng byte đầu vào.

Dưới đây là một minh chứng về điều tương tự -

Giả sử đầu vào của chúng tôi là -

Input string: Java Program

Đầu ra mong muốn sẽ là -

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

Thuật toán

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

Ví dụ 1

Ở đây, chúng tôi liên kết tất cả các hoạt động với nhau trong hàm "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();
      }
   }
}

Đầu ra

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

Ví dụ 2

Ở đây, chúng tôi đóng gói các hoạt động thành các hàm thể hiện lập trình hướng đối tượng.

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

Đầu ra

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