1、InputStream转化为String 1.1 JDK原生提供 方法一: byte[] bytes = new byte[0]; bytes = new byte[inputStream.available()]; inputStream.read(bytes); String str = new String(bytes); 方法二: String result = new BufferedReader(new InputStreamReader(inputStream)) .lines().collect(Collectors....
InputStream in_withcode = new ByteArrayInputStream(str.getBytes("UTF-8")); 2.InputStream to String 这里提供几个方法。 方法1: public String convertStreamToString(InputStream is) { BufferedReader reader = new BufferedReader(new InputStreamReader(is)); StringBuilder sb = new StringBuilder(); Str...
importjava.io.ByteArrayInputStream;importjava.io.InputStream;publicclassStringToInputStream{publicstaticvoidmain(String[]args){// 创建一个字符串Stringstr="Hello, World!";// 这个字符串将被转换为InputStream// 将字符串转换为InputStreamInputStreaminputStream=newByteArrayInputStream(str.getBytes());// ...
转换过程需要借助ByteArrayInputStream读取字符串的字节码,ByteArrayInputStream是InputStream的子类,强制转换即可。 代码如下: String template="abcdef"; ByteArrayInputStream byteArrayInputStream=newByteArrayInputStream(template.getBytes()); InputStream inputStream=(InputStream)byteArrayInputStream;...
2. InputStream --> String String inputStream2String(InputStream is){ BufferedReader in = new BufferedReader(new InputStreamReader(is));StringBuffer buffer = new StringBuffer();String line = "";while ((line = in.readLine()) != null){ buffer.append(line);} return buffer.toString();} ...
1、String –> InputStream InputStrem is = new ByteArrayInputStream(str.getBytes()); 或者 ByteArrayInputStream stream= new ByteArrayInputStream(str.getBytes()); 2、InputStream–>String inputStream input = null; StringBuffer out = new StringBuffer(); ...
InputStream --> String String inputStream2String(InputStream is){ BufferedReader in = new BufferedReader(new InputStreamReader(is));StringBuffer buffer = new StringBuffer();String line = "";while ((line = in.readLine()) != null){ buffer.append(line);} return buffer.toString();} File ...
String result = new BufferedReader(new InputStreamReader(inputStream)).lines().collect(Collectors.joining("\n"));使用 parallel Stream API (Java 8). 警告: 这个解决方案将不同的换行符(如\r\n)转换为\n。String result = new BufferedReader(new InputStreamReader(inputStream)).lines().parallel()....
5. 使用parallel Stream Api (Java 8). 提醒: 这种方式会将不同的换行符 (比如\r\n) 都替换为 \n. String result=newBufferedReader(newInputStreamReader(inputStream)).lines().parallel().collect(Collectors.joining("\n")); 6. 使用InputStreamReader 和StringBuilder (JDK) ...
java里面InputStream类型转换成String类型,主要是通过读入文件的字符,拼接成String类型,如下代码:InputStream is = new FileInputStream("D://123.txt");int i = 0;while( (i = is.read())!= -1){System.out.print((char)i);//输出char类型}得到的char一个一个接起来就是string ...