给定一个字符串:
String exampleString = "example";
如何将其转换为 InputStream?
像这样:
InputStream stream = new ByteArrayInputStream(exampleString.getBytes(StandardCharsets.UTF_8));
请注意,这假设您需要一个 InputStream,它是一个字节流,表示编码为 UTF-8 的原始字符串.
Note that this assumes that you want an InputStream that is a stream of bytes that represent your original string encoded as UTF-8.
对于低于 7 的 Java 版本,将 StandardCharsets.UTF_8 替换为 "UTF-8".
For versions of Java less than 7, replace StandardCharsets.UTF_8 with "UTF-8".
这篇关于如何在 Java 中将字符串转换为 InputStream?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
“Char 不能被取消引用"错误quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch 语句 - 是“或"/“和"可能的?Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java替换字符串特定位置的字符?Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
具有 int 和 char 操作数的三元表达式的类型是什么What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
读取文本文件并存储出现的每个字符Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
为什么我需要在 byte 和 short 上显式转换 char 原语Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)