如何从输入流中读取数据到字符串中
来源:互联网 发布:网络可用情况发生变化 编辑:程序博客网 时间:2024/04/30 11:31
一、读取思路
1.使用InputStream对象构造InputStreamReader
2.使用InputStreamReader对象构造BufferedReader对象
3.构造一个StringBuilder对象,用来接收BufferedReader中的数据
二、源代码
@Testpublic void test3() throws Exception, IOException{CloseableHttpClient httpclient = HttpClients.createDefault();HttpGet httpget = new HttpGet("url");CloseableHttpResponse response = httpclient.execute(httpget);try {HttpEntity entity = response.getEntity();if (entity != null) {//创建一个输入流对象 InputStream instream = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } instream.close(); System.out.println(sb.toString()); //如何从InputStream中读取数据到字符串//从输入流中读取数据/*int byteOne = instream.read();int byteTwo = instream.read();System.out.println(byteOne);System.out.println(byteTwo);*/} } finally { response.close(); }}
0 0
- 如何从输入流中读取数据到字符串中
- 从标准输入流中读取数据
- 从标准输入流中读取数据,并存储到文件中
- Java, 如何从Excel中读取数据到文件中
- 如何从文件中读取字符串到string对象
- 【C++】从标准输入中读取字符串
- 如何从XML文件中读取数据绑定到DropDownList
- R语言如何从外部读取数据到R中
- std::getline从输入流中读取多行字符串
- 使用字符串输入流从字符串中提取数据
- 如何从一串字符串中读取数字
- Java如何从数据中读取数据,然后插入到指定的文本中
- 从资源读取数据到文件中
- 从数据库中读取数据到Excel
- 从文档读取数据到list中
- 如何从数据库中读取数据
- 如何从property文件中读取数据
- C++从txt文本中输入和读取字符串
- HTML5 CSS 结构_2
- Widget-CircleImageView解析
- acm刷题目录
- 仿QQ空间文章列表+评论查询
- R第五问 R语言扩展包dplyr笔记
- 如何从输入流中读取数据到字符串中
- 题目56 阶乘因式分解(一)
- ios开发应用程序,如何限制只能竖屏展示?旋转横屏后界面无变化
- 安装android sdk5.0后出现 android sdk Content Loader's has encountered a problem
- JavaScript时间整理
- List转String时注意事项
- 树状数组 和 归并排序 求逆序数
- React 学习索引
- rtmpdump 移植记录 与 简单使用