Read/convert an InputStream to a String

来源:互联网 发布:爱情不是毒药网络歌手 编辑:程序博客网 时间:2024/05/16 07:49

stackoverflow网站上的一个问题。

怎样用最简单的方法将inputstream转化为string类型?

比较好的答案:

public class InputStreamToString {public static void main(String[] args) {FileInputStream inputStream = null;StringWriter writer = null;try {inputStream = new FileInputStream(new File("C:\\Users\\Administrator\\Desktop\\cid.txt"));writer = new StringWriter();IOUtils.copy(inputStream, writer);String string = writer.toString();System.out.println(string);} catch (FileNotFoundException e1) {e1.printStackTrace();} catch (IOException e) {e.printStackTrace();}finally {try {inputStream.close();writer.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}



0 0
原创粉丝点击