English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Java Basic Tutorial

Contrôle de flux Java

Tableau Java

Java orienté objet (I)

Java orienté objet (II)

Java orienté objet (III)

Java Exception Handling

Liste (List) Java

Queue (file d'attente) Java

Ensemble Map Java

Ensemble Set Java

Flux d'entrée/sortie (I/O) Java/O)

Reader Java/Writer

Autres sujets Java

Programme Java convertissant une chaîne en InputStream

Java example大全

Dans ce programme, nous allons apprendre à convertir une chaîne en flux d'entrée dans Java.

Pour comprendre cet exemple, vous devriez comprendre ce qui suitProgrammation JavaThème :

Exemple : programme Java pour convertir une chaîne en InputStream

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class Main {
  public static void main(String args[]) {
    //Créer une chaîne de caractères
    String name = "w3codebox";
    System.out.println("La chaîne de caractères est: ") + name);
    try {
      InputStream stream = new ByteArrayInputStream(name.getBytes(StandardCharsets.UTF_)8));
      System.out.println("InputStream: ") + stream);
      //Return the number of available bytes
      System.out.println("Début des octets disponibles: ") + stream.available());
      //Read from the stream stream3bytes
      stream.read();
      stream.read();
      stream.read();
      //reading3bytes after
      //Return the number of available bytes
      System.out.println("Last available byte: ", + stream.available());
      stream.close();
    }
    catch (Exception e) {
      e.getStackTrace();
    }
  }
}

Output result

The string is: w3codebox
InputStream: java.io.ByteArrayInputStream5479e3f
Starting available byte: 5
Last available byte: 2

In the above example, we created a string named name. Here, we convert the string to an input stream named stream.

The getBytes() method converts a string to bytes. For more information, please visitJava String getBytes()

Java example大全