node.js - java的輸入流、輸出流怎么理解?
問題描述
import java.net.*;import java.io.*;public class URLConnDemo{ public static void main(String [] args) { try { URL url = new URL('http://www.xxx.com'); URLConnection urlConnection = url.openConnection(); HttpURLConnection connection = null; if(urlConnection instanceof HttpURLConnection) { connection = (HttpURLConnection) urlConnection; } else { System.out.println('請輸入 URL 地址'); return; } BufferedReader in = new BufferedReader( new InputStreamReader(connection.getInputStream())); String urlString = ''; String current; while((current = in.readLine()) != null) { urlString += current; } System.out.println(urlString); }catch(IOException e) { e.printStackTrace(); } }}
從這段代碼來看,請求一個url并把內(nèi)容讀取出來顯示,但是為什么這里用到getInputStream,應(yīng)該不是getOutStream 輸出嗎?
問題解答
回答1:InputStream 是用來讀取的,OutputStream 是用來寫入的;換句話說,輸入流是指輸入到系統(tǒng)中的流,系統(tǒng)從這個流中讀取內(nèi)容;輸出流是指從系統(tǒng)輸出的流,系統(tǒng)往這個流中寫入內(nèi)容。這個取名方式是站在使用者的角度,而不是 Stream 對象的角度。用過幾次就習慣了。
相關(guān)文章:
1. mysql - 如何減少使用或者不用LEFT JOIN查詢?2. html5 - H5 audio 微信端 在IOS上不能播放音樂3. python - 編碼問題求助4. python - 我在使用pip install -r requirements.txt下載時,為什么部分能下載,部分不能下載5. 視頻文件不能播放,怎么辦?6. mysql - 分庫分表、分區(qū)、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處7. mysql - jdbc的問題8. python - Scrapy存在內(nèi)存泄漏的問題。9. Python爬蟲如何爬取span和span中間的內(nèi)容并分別存入字典里?10. mysql - 千萬級數(shù)據(jù)的表,添加unique約束,insert會不會很慢?
