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