java里面InputStream类型转换成String类型怎么实现
发布网友
发布时间:2022-04-22 18:57
我来回答
共5个回答
热心网友
时间:2023-07-16 20:58
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
热心网友
时间:2023-07-16 20:58
.toString().valueOf()
热心网友
时间:2023-07-16 20:59
InputStream in = new InputStream(***);
InputStreamReader is = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(is);
String tmp = null;
while((tmp=reader.readLine())!=null)
System.out.println(tmp);
热心网友
时间:2023-07-16 20:59
InputStream is = new FileInputStream("D://123.txt");
int i = 0;
while( (i = is.read())!= -1){
System.out.print((char)i);
}
得到的char一个一个接起来就是string 了
热心网友
时间:2023-07-16 21:00
byte buffer[] = new byte[1024];
int len = 0;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
while ((len = inputstream.read(buffer)) != -1) {
baos.write(buffer, 0, len);
}
baos.close();
inputstream.close();
byte[] data =baos.toByteArray();
String result = new String(data, "utf-8");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
inputstream就是你需要转换成string类型的流