如何從輸入流中讀取資料到字串中
一、讀取思路
1.使用InputStream物件構造InputStreamReader
2.使用InputStreamReader物件構造BufferedReader物件
3.構造一個StringBuilder物件,用來接收BufferedReader中的資料
二、原始碼
@Test public 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(); } }
相關推薦
C++opencv中讀取資料夾中的多幅圖片並按順序重新命名後儲存到另一資料夾中
#include <iostream> #include <fstream> #include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include &
如何從輸入流中讀取資料到字串中
一、讀取思路 1.使用InputStream物件構造InputStreamReader 2.使用InputStreamReader物件構造BufferedReader物件 3.構造一個StringBuilder物件,用來接收BufferedReader中的資料 二、原始碼
從標準輸入流中讀取資料
package chapter10.branch01;import java.io.BufferedReader;import java.io.InputStreamReader;public class TestStandarInput { public static v
從HttpServletRequest request, 中讀取資料流中的內容
BufferedReader in=new BufferedReader(new InputStreamReader(request.getInputStream())); StringBuilder sb = new StringBuilder();
python 從檔案中讀取資料,同時去除掉空格和換行
從檔案中讀取資料,同時去除掉空格和換行,程式碼如下 import numpy as np def sort(path): w = open(path,'r') l = w.readlines() col=[] for k in l: k = k.strip('\n')
【120】TensorFlow 從CSV檔案中讀取資料並訓練線性迴歸模型(面向新手)
正文開始。 學習 TensorFlow 讓我的思維發生了變化。 計算機本質上是一種數學的工具,而我在學習程式設計的時候,思維也不可避免地收到了影響。傳統的程式設計思想,常常認為程式就應該像數學定理或者數學函式一樣,給出一個確定的結果。這是一種基於邏輯推導
Prefuse學習(二)從資料庫中讀取資料
prefuse是一個非常不錯的開源視覺化專案,尤其是用在social network/complex network上,個人感覺要比jung好。不過可惜的是,prefuse的user manual還在建
怎麼從excel中讀取資料_python
下載擴充套件庫 xlrd 讀excle xlwt 寫excle 直接在百度上搜就能下載 下載後使用 import xlrd 就可以讀excle檔案了 開啟檔案: xls = xlrd.open_workbook(fileName) 選擇表:括號內為第幾個表 sheet
資料庫配置讀寫分離後如何方便地從主庫中讀取資料
基於ThinkPHP5.1.15+MySQL演示主從同步配置與讀寫分離解決方案。 軟體環境:ThinkPHP5.1.15+MariaDB 10.1.30(主伺服器)+MySQL5.7(從伺服器)+Win10 兩臺伺服器IP分別為:192.168.199.234、192.1
用python從excel表格中讀取資料生成可以放在科技論文中的圖片
最近在寫畢業小論文,需要插入符合科技論文的資料圖,其實很多的設定都是規定好的,雖然自己在excel中設定一下也不花多少時間,覺得好玩想著是不是可以用python做一個自動讀取資料畫圖的小指令碼,以後寫報告也可以用得著。 其實整個思路很清楚,兩個晚上的樣子就搭好了大體的程式,
PHP 從txt檔案中讀取資料
一、開啟/關閉檔案 1、對檔案操作時首先要開啟檔案,開啟檔案用 fopen()函式,語法是: fopen(filename,mode,include_path,context); 2、對檔案操作結束後應該關閉這個檔案,使用函式 fclose(); 例如: 二、讀寫檔案 1、讀取整個檔案,有
Java從CSV檔案中讀取資料和寫入
.CSV檔案是以逗號分割的資料倉儲,讀取資料時從每一行中讀取一條資料元祖,也就是一條資料,再用字元分割的方式獲取表中的每一個數據項。 package com.conn.csv;
關於python從Oracle中讀取資料中文全是問號的問題
用python連線Oracle資料庫,結果讀取出來的資料,中文部分全是問號,利用: import chardet print(chardet.detect(i)) 發現是ASCII,用了encode和decode來進行轉碼,結果都沒用,最後找到一個帖子,終於將問題解決,只要在程式碼中新增:
spark叢集從HDFS中讀取資料並計算
一、 利用spark從hadoop的hdfs中讀取資料並計算 1.1準備階段 部署好hadoop分散式搭建(+zookeeper,6臺機器)可以參考這篇部落格:http://blog.csdn.net/vinsuan1993/article/deta
python matplotlib從檔案中讀取資料繪製散點圖
示例說明:從一個檔案讀取資料,繪製成散點圖 #coding:utf-8 import matplotlib.pyplot as plt import numpy as np import matpl
ffmpeg 從記憶體中讀取資料(或將資料輸出到記憶體)
原文見雷大神部落格:http://blog.csdn.net/leixiaohua1020/article/details/12980423 更新記錄(2014.7.24): 1.為了使本文更通俗易懂,更新了部分內容,將例子改為從記憶體中開啟。 2.增加了將資料輸出
android 將資料儲存到檔案中&從檔案中讀取資料
1.將資料儲存到檔案中,我直接貼程式碼了,基本註釋都在程式碼中,原諒我很懶。 /** * 將資料存到檔案中 * * @param context context
一個簡單的從txt檔案中讀取資料插入資料庫
一: 首先我們應該先·建一個Dynamic web project 工程。 二: 定義一個讀檔案操作 package com.what.files; 在這裡我們要用到用於讀取本地檔案中的位元組資料的FileInputStream,FileInputStream繼承了Inpu
使用RandomAccessFile這個類,從檔案中讀取資料,卻讀取不出來
請看下面這段程式碼,相關的問題我應經註釋了出來: package timemanager; import java.io.EOFException; import java.io.File; import java.io.FileNotFoundException; im
從多個tfrecord檔案中讀取資料的方法
tfrecord_file_path = '/train/*.tfrecords’#train是存放tfrecord的資料夾 filename_queue = tf.train.string_input_producer( tf.tr