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并把內容讀取出來顯示,但是為什么這里用到getInputStream,應該不是getOutStream 輸出嗎?
問題解答
回答1:InputStream 是用來讀取的,OutputStream 是用來寫入的;換句話說,輸入流是指輸入到系統中的流,系統從這個流中讀取內容;輸出流是指從系統輸出的流,系統往這個流中寫入內容。這個取名方式是站在使用者的角度,而不是 Stream 對象的角度。用過幾次就習慣了。
相關文章:
1. boot2docker無法啟動2. docker-compose中volumes的問題3. 關docker hub上有些鏡像的tag被標記““This image has vulnerabilities””4. docker容器呢SSH為什么連不通呢?5. java - SSH框架中寫分頁時service層中不能注入分頁類6. dockerfile - 為什么docker容器啟動不了?7. 關于docker下的nginx壓力測試8. node.js - antdesign怎么集合react-redux對input控件進行初始化賦值9. docker安裝后出現Cannot connect to the Docker daemon.10. nignx - docker內nginx 80端口被占用

網公網安備