有没有办法在Java中进行此转换?我已经尝试在线建议将字符串转换为Inputstream类型,但这并不相同,我在网上找不到其他东西。
这可能会有所帮助
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.InputStream;
import java.nio.charset.Charset;
public class MyApp {
public static void main(String[] args) throws Exception {
InputStream is = new ByteArrayInputStream("hi I am test".getBytes(Charset.forName("UTF-8")));
DataInputStream dataIn = new DataInputStream(is);
while (dataIn.available() > 0) {
String k = dataIn.readLine();
System.out.print(k + " ");
}
}
}
希望下面的希望将帮助您将String
转换为DataInputStream
String string = "Sample String";
InputStream inputStream = new ByteArrayInputStream(string.getBytes(Charset.forName("UTF-8")));
DataInputStream dis = new DataInputStream(inputStream);